The if Statement

So far, your programs have run every line, one after another, from top to bottom. But a program often has to decide what to do: water the field only if it hasn't rained, or take the eggs to the market only if there are enough of them. People decide like this all the time: if it rains, I'll take an umbrella.

In Lua, you write a decision with the if statement:

local eggs = 14
if eggs > 12 then
    print("Enough eggs for the market")
end
print("Eggs in the basket: " .. eggs)

This prints:

Enough eggs for the market
Eggs in the basket: 14

The if statement has these parts:

  • the keyword if. A keyword is a word with a special meaning in Lua, so you can't use it as a variable name;
  • a condition: eggs > 12. A condition is a question that can only be answered yes (true) or no (false): are there more than 12 eggs?
  • the keyword then, which ends the condition;
  • a block of code: the lines between then and end. The block runs only if the condition is true;
  • the keyword end, which ends the block and the whole if statement.

After end, the program goes on as usual, so Eggs in the basket: 14 is printed either way. If eggs were 8, the condition would be false: the program would skip the block and print only Eggs in the basket: 8.

In a condition, you can compare with > (greater than) and < (less than), as in math. You'll meet the other comparisons in the next exercise.

The lines of the block are indented: they start with 4 spaces. Lua doesn't need that, because then and end decide what belongs to the block. Indentation is for people: it shows at a glance which lines run only sometimes, so always use it. The Tab key adds 4 spaces, and when you press Enter, the new line keeps the indentation of the line above; before end, delete it.

The most common mistakes are a missing then and a missing end. Lua finds both before it runs anything, so the program prints nothing:

local eggs = 14
if eggs > 12
    print("Enough eggs for the market")
end
main.lua:3: 'then' expected near 'print'

The error points to line 3, because Lua only finds out that then is missing when it reaches print. Without end, Lua gets to the end of the program, <eof> (“end of file”), and tells you which if isn't closed:

local eggs = 14
if eggs > 12 then
    print("Enough eggs for the market")
print("Eggs in the basket: " .. eggs)
main.lua:4: 'end' expected (to close 'if' at line 2) near <eof>

Instructions

Below the last line, write an if statement with the condition water > 20. In its block, print Emma can water the whole field. The program prints:

Water in the well: 30 buckets
Emma can water the whole field

Below its end, write a second if statement with the condition water < 10 that prints The well is almost empty. Run the program: it still prints only two lines, because 30 isn't less than 10, so the second block is skipped.

Change water to 5 and run the program. Now the first condition is false and the second one is true, so the program prints:

Water in the well: 5 buckets
The well is almost empty

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/10