Rob Cook


Home | GitHub | Dev.to | Donate | Contact

[2] A little bit of Lua


2026-01-05

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.

A complete program with conditional logic

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.

Using elseif to check for other conditions

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

Exercises

  1. Modify the second program listing to also check for the name "carol". Print a suitable message if the name is found.

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

Answers

  1. We can achieve this with another 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
  1. Changing the line to use 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
end