Jump to content




multiple else statements?


  • You cannot reply to this topic
13 replies to this topic

#1 cheekycharlie101

  • Members
  • 231 posts

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.

#2 ChunLing

  • Members
  • 2,027 posts

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 ?

#3 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 29 October 2012 - 12:06 PM

.

#4 Jasonfran

  • Members
  • 148 posts
  • LocationEngland

Posted 29 October 2012 - 02:38 PM

You could try some elseifs too

#5 cheekycharlie101

  • Members
  • 231 posts

Posted 29 October 2012 - 04:29 PM

View PostJasonfran, on 29 October 2012 - 02:38 PM, said:

You could try some elseifs too


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

View Postjag_e_nummer_ett, on 29 October 2012 - 12:06 PM, said:

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.?

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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.

#7 cheekycharlie101

  • Members
  • 231 posts

Posted 29 October 2012 - 09:42 PM

View PostsIdEkIcK_, on 29 October 2012 - 05:27 PM, said:

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

#8 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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


#9 cheekycharlie101

  • Members
  • 231 posts

Posted 30 October 2012 - 12:58 AM

Thanks Guys soooo much, you helped me clean up my disk manager code SOOOOOOOOOOOOO much:D

#10 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 30 October 2012 - 12:54 PM

.

#11 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 30 October 2012 - 08:32 PM

View Postjag_e_nummer_ett, on 30 October 2012 - 12:54 PM, said:

View Postluanub, on 29 October 2012 - 09:53 PM, said:

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".

#12 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 31 October 2012 - 01:56 AM

.

#13 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 31 October 2012 - 02:00 AM

View Postcheekycharlie101, on 29 October 2012 - 09:42 PM, said:

View PostsIdEkIcK_, on 29 October 2012 - 05:27 PM, said:

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

View Postjag_e_nummer_ett, on 31 October 2012 - 01:56 AM, said:

View Postluanub, on 30 October 2012 - 08:32 PM, said:

View Postjag_e_nummer_ett, on 30 October 2012 - 12:54 PM, said:

View Postluanub, on 29 October 2012 - 09:53 PM, said:

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.

#14 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 31 October 2012 - 02:09 AM

Call it rage quit or whatever you want, but I removed my posts. (Well kind of)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users