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

Monitor control

Started by Uekyq, 05 August 2012 - 03:58 AM
Uekyq #1
Posted 05 August 2012 - 05:58 AM
Hey, I have a system that I need to set up. How do you have an if/else command have multiple ifs and only one else? like if you type 1 it prints hello, if you type 2, it prints hey, if you type hello it says 'what?'. I tried to figure this out but I can't figure it out.
ElvishJerricco #2
Posted 05 August 2012 - 06:15 AM
you're looking for the elseif statement


str = read()
if str == "1" then
	print("hello")
elseif str == "2" then
	print("hey")
elseif str == "hello" then
	print("what?")
else
	print("Sorry i didn't recognize what you typed")
end

notice that you still have to use "then" after elseif, and that elseif is essentially just saying "if the previous if statement was not true, then test for this one." So if two of your elseif chunks of code are true, only the first one will be run. Such as:


str = "hey"
var = 23
if str == "hey" then
	print("hi!")
elseif var == 23 then
	print("24!")
end

Only the first one will be run because although they're both true, the second one relies on the first to be false in order to run.