For a program to be truly useful, it needs to be able to make decisions. This
allows us to conditionally run some code, based on whether certain values are
provided to the program. In Lua, the if statement is used to check a condition
and run certain code if the condition is considered to be true.
For example, in the following code snippet the words "Hi, there!" will only be
printed out if the variable name contains the word "Alice":
if name == "Alice" then
io.write("Hi, there!\n")
end
The check name == "Alice" is the conditional. The double equals == is Lua's
way of asking if two values are the same as each other. The lines between the
then and end keywords are run if the conditional is true. You can have as
many lines as you like between the then and end.
Lets look at a complete program using a conditional check.
-- If statements and comparisons.
io.write("Enter your name: ")
local name = io.read()
name = string.lower(name)
if name == "alice" then
io.write("Alice, good to see you again!\n")
else
io.write("Hello " .. name .. "!\n")
end
In this listing we have also introduced the else keyword. It says if the
conditional was not true (false), then run these other lines (between the else
and the end). The else is an optional part of the if statement.
The other new thing is the line name = string.lower(name). This calls the
function string.lower - which changes the contents of the variable name to be
all lowercase. The equals sign = says to assign the new lowercase value back
to the name variable (overwriting what was in it).
Run this program twice. First inputting the name "Alice" then a different name. Notice how the program outputs different messages depending on the name used.
elseif to check for other conditionsIn this next listing, we add a new keyword to the if else chain - the elseif
statement. This allows us to introduce another conditional, in this case checking
if the variable name is equal to the value "bob".
-- If statements and comparisons.
io.write("Enter your name: ")
local name = io.read()
name = string.lower(name)
if name == "alice" then
io.write("Alice, good to see you again!\n")
elseif name == "bob" then
io.write("Bob! It's been a long time.\n")
else
io.write("Hello " .. name .. "!\n")
end
We read this chain of statements as: If the name is "alice" do this; else if the
name is "bob" do this; else do this. An elseif is also an optional part of the if statement.
Modify the second program listing to also check for the name "carol". Print a suitable message if the name is found.
Change the line name = string.lower(name) to be name = string.upper(name).
What effect does this have on the conditional statements? How could you change
them to make the program work correctly again?
elseif condition like in this listing:if name == "alice" then
io.write("Alice, good to see you again!\n")
elseif name == "bob" then
io.write("Bob! It's been a long time.\n")
elseif name == "carol" then
io.write("Carol, nice to meet you.\n")
else
io.write("Hello " .. name .. "!\n")
end
upper instead of lower will cause the name to be
changed to uppercase. We therefore need to modify the values in our conditionals
to use uppercase letters like this listing:-- If statements and comparisons.
io.write("Enter your name: ")
local name = io.read()
name = string.upper(name)
if name == "ALICE" then
io.write("Alice, good to see you again!\n")
elseif name == "BOB" then
io.write("Bob! It's been a long time.\n")
else
io.write("Hello " .. name .. "!\n")
end