Lua: If, Else, ElseIf
Often when programming, you want to compare one value to another (wouldn't it be useful if we could take information from io.read, and then check what the user has written?). We can accomplish this concept in Lua using these things called "if statements".
If statements consist of the keyword "if", followed by a condition (we'll figure out how to formulate these in a minute) followed by the "then" keyword, then some code we want to execute if the condition is true, followed by the "end" keyword (to tell it we've finished the if statement). So the concept looks something like this:
if CONDITION then --Code to execute if true end
So the real gap in there, is the condition. So how do we formulate it?
Well. A typical condition consists of a value (it could be a variable (such as "number") or just a constant value like "5"), a conditional operator (such as the "is equal to" operator – we'll learn what this is in a minute), and then another value.
There are a wide variety of conditional operators built in to Lua, but the most basic is the "is equal to" operator – indicated by two equals signs ("==").
So if we wanted to check if the variable 'number' is equal to 5, we'd want to formulate a condition like this one:
number == 5
So now we have a condition, we can create an entire if statement!
number = 3 if number == 5 then --Code to execute if true end
Other conditional operators include "greater than" (">"), "less than" ("<"), "greater than or equal to" (">="), "less than or equal to" ("<="), and "not equal to" ("~="). These are all pretty self-explanatory and are used between two values, just as we did with "is equal to" ("==") in the example code snippet above. Try replacing the conditional operators we use in code snippets below with some of the ones shown above, and see what happens! (You'll gain a lot of knowledge and experience from just playing around with code)
In the case of the above code snippet though, it's obviously pretty useless. We're setting a variable to a constant value and then checking if it equals another constant value – however, if statements become very useful when we use them with varying values, such as when we are handling user input.
So let's say we wanted to take the user's input, and check if they were saying "Hi" to us. It's worth noting that user input is taken by io.read as a string data type (we've kind of brushed over this before – note that if you want to compare a string to another data-type, you need to convert the string to a different type, we'll learn about this in some future tutorial), this means that any conditions that are comparing the string, must compare it to another string. Remember than string constants are defined by double quotes, so getting the user input using io.read and checking if it's "Hi" – might look something like this:
io.write("Enter a message: ")
userinput = io.read()
if userinput == "Hi" then
print("Well howdy there!")
end
On running this, you can see that it works (note that it's case sensitive (for now! We can fix this in later tutorials)). But what can we do if we something else to happen if userinput doesn't equal "Hi", but equals something else – like "Bye".
Well, we could do another if statement after the one above to check if it's "Bye", like so:
io.write("Enter a message: ")
userinput = io.read()
if userinput == "Hi" then
print("Well howdy there!")
end
if userinput == "Bye" then
print("Goodbye!")
end
However the above method is definitely not the best way to do things! It's certainly not efficient, because if "userinput" is "Hi" it is not "Bye", so our computer shouldn't have to check if it's "Bye" after determining that it's "Hi"!
The way around this is something called "elseif". It "daisy chains" on from an if statement and checks another condition, if the first is not true. It is accomplished by replacing the "end" that we had previously, with "elseif", specifying a condition, using the "then" keyword, writing the code you want to execute if the "elseif" condition is true, and then writing our "end" keyword to tell it we've finished.
So we could replace the inefficient code snippet shown above, with the more efficient code as follows:
io.write("Enter a message: ")
userinput = io.read()
if userinput == "Hi" then
print("Well howdy there!")
elseif userinput == "Bye" then
print("Goodbye!")
end
Ah.. That's much better!
We can actually "daisy chain" these as much as we want, so we can have a (theoretically) infinite amount of elseifs! For example:
io.write("Enter a message: ")
userinput = io.read()
if userinput == "Hi" then
print("Well howdy there!")
elseif userinput == "Bye" then
print("Goodbye!")
elseif userinput == "Hello" then
print("Hi!")
elseif userinput == "Hu" then
print("Spelling error much?")
end
On running the program, you can see that the program reacts differently depending on what the user inputs in!
So we have a chain of if and else-ifs, but how do we specify what it should do if none of the conditions are met? Surely there is a way to add this functionality into our "daisy chain"?
Of course there is!
It's called "else".
Much like the "elseif", we can replace "end" with "else" and then push the "end" to the end of our chain. Of course we don't need to specify a condition, because "else" by definition doesn't require a condition. The code we want to execute in the "else" condition simply goes between the "else" and "end".
So in the case of our very simply "AI" type program, we might want something like this:
io.write("Enter a message: ")
userinput = io.read()
if userinput == "Hi" then
print("Well howdy there!")
elseif userinput == "Bye" then
print("Goodbye!")
elseif userinput == "Hello" then
print("Hi!")
elseif userinput == "Hu" then
print("Spelling error much?")
else
print("I don't understand you. Sorry.")
end
Back to Lua

