This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cheekycharlie101's profile picture

multiple else statements?

Started by cheekycharlie101, 29 October 2012 - 10:45 AM
cheekycharlie101 #1
Posted 29 October 2012 - 11:45 AM
ok, i doubt any of you are going to be able to answer this because of lack of info from me. but a while ago i was coding (cant remember what) and i tried using multiple else statments. il try and make an example.


local a = peripheral.isPresent("left")
local b = disk.isPresent("left")
if a == true then
print("drive found")
if b == true then
print("disk found")
else
print("drive not found")
else
print("disk not found")
i

so i need this to say weather a disk is found or a drive is found or if there not found. but with this code im pretty sure it would not work. how could i get this to check for a drive, if a drive is found check for a disk. if a disk is not found say. if a drive is not found say and end the program? if someone could help me with this using multiple else statements i would be really grateful.
thanks -Cheeky

if your wondering what this code is for its for a disk manager im making.
ChunLing #2
Posted 29 October 2012 - 11:59 AM
You need some ends for those ifs. Are you going for an if then if then else end else end structure, or are you wanting to use if then elseif then else end ?
jag #3
Posted 29 October 2012 - 12:06 PM
.
Jasonfran #4
Posted 29 October 2012 - 02:38 PM
You could try some elseifs too
cheekycharlie101 #5
Posted 29 October 2012 - 04:29 PM
You could try some elseifs too


thought about that, but it didn't work out too well when i put it into practise.

This would fit better:

local a = peripheral.isPresent("left")
local b = disk.isPresent("left")
if a == true then
  print("drive found")
  if b == true then
	print("disk found")
  else
	print("disk not found")
  end
else
  print("drive not found")
end

thats basically what i wrote with indents:P but would this work, im aware i missed out an end.?
remiX #6
Posted 29 October 2012 - 05:27 PM
Yes it will work, but you can actually just put if a then – because it returns a Boolean.
cheekycharlie101 #7
Posted 29 October 2012 - 09:42 PM
Yes it will work, but you can actually just put if a then – because it returns a Boolean.

i dont really understand, can u code up a quick example:P
Luanub #8
Posted 29 October 2012 - 09:53 PM

local a = peripheral.isPresent("left")
local b = disk.isPresent("left")
if a then
  print("drive found")
  if b then
    print("disk found")
  else
    print("disk not found")
  end
else
  print("drive not found")
end
cheekycharlie101 #9
Posted 30 October 2012 - 12:58 AM
Thanks Guys soooo much, you helped me clean up my disk manager code SOOOOOOOOOOOOO much:D
jag #10
Posted 30 October 2012 - 12:54 PM
.
Luanub #11
Posted 30 October 2012 - 08:32 PM

local a = peripheral.isPresent("left")
local b = disk.isPresent("left")
if a then
  print("drive found")
  if b then
	print("disk found")
  else
	print("disk not found")
  end
else
  print("drive not found")
end
I think I just said that

Read up a little, he wanted an example of "if a then" instead of "if a == true then".
jag #12
Posted 31 October 2012 - 01:56 AM
.
remiX #13
Posted 31 October 2012 - 02:00 AM
Yes it will work, but you can actually just put if a then – because it returns a Boolean.

i dont really understand, can u code up a quick example:P

local a = peripheral.isPresent("left")
local b = disk.isPresent("left")
if a then
  print("drive found")
  if b then
	print("disk found")
  else
	print("disk not found")
  end
else
  print("drive not found")
end
I think I just said that

Read up a little, he wanted an example of "if a then" instead of "if a == true then".
I can remove my code if that is what you want.
Because, both our codes does the same thing and I can't find in his description that he specifically wants "if a then".

I told him about it then he asked how it can be done with an example of a code.
jag #14
Posted 31 October 2012 - 02:09 AM
Call it rage quit or whatever you want, but I removed my posts. (Well kind of)