Control Flow
By the end of this lesson you'll be able to make decisions with if / elseif / else , compare and combine values with Lua's word-based operators, and repeat work with every kind of Lua loop — while side-stepping the truthiness trap that catches developers from every other language.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Lua course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Think of control flow as the signs and roundabouts on a road trip. An if is a junction: "is the light red? then stop, else go." A loop is a roundabout you keep circling until a condition lets you exit — break is the exit ramp you take the moment you spot your turn. Your code, like a car, only ever drives ONE path at a time; the conditions decide which. Get the signs right and the journey runs itself; get one backwards and you loop forever.
1. Making Decisions with if / elseif / else
An if runs a block of code only when a condition is true . Add elseif branches to test more conditions, and a final else to catch everything left over. Lua checks the tests top to bottom and runs the first one that is true, then jumps to end . Two things to burn in now: the branch keyword is the single word elseif (not else if ), and every if must be closed with end . Read this worked example, then you'll write your own.
2. Relational Operators (and the ~= surprise)
Conditions are usually built from relational operators that compare two values and return true or false . Most are familiar: , , , , and == for "equal to". The one to memorise is "not equal" : in Lua it is ~= (tilde-equals), not != . Writing != is a syntax error, so pair them in your head: equal is == , not-equal is ~= .
3. Combining Conditions: and / or / not
To join conditions, Lua uses words , not symbols: and , or , and not (there is no && , || , or ! ). and needs both sides true; or needs at least one; not flips a value. Lua also has no ternary operator, so the community idiom cond and a or b stands in for "if cond then a else b" — handy for picking one of two values on a single line.
In Lua, the only falsy values are nil and false . Everything else is truthy — including the number 0 , the empty string "" , and an empty table {' '} . If you come from JavaScript, Python, or C, this is backwards from what you expect, where 0 and "" are falsy.
So to check for zero you must be explicit — write if count == 0 then , never rely on 0 being falsy.
The program below is almost complete — fill in the three blanks marked ___ using the hints in the comments, then run it and check your output matches.
4. Repeating Work with Loops
A loop runs a block of code over and over. Lua gives you three forms. The numeric for counts: for i = start, stop, step do (the step is optional and defaults to 1). The while loop repeats while a condition stays true, checking it at the top. The repeat...until loop runs the body first and tests at the bottom, so it always runs at least once. Note Lua has no ++ or -= , so you update counters in full like fuel = fuel - 1 .
5. Looping Over Tables: pairs & ipairs
The generic for walks over a table. Use ipairs(t) for list-style tables — it visits integer keys 1, 2, 3... in order and stops at the first gap. Use pairs(t) to visit every key, including string keys like "name" , though the order is not guaranteed. Exit any loop early with break . There is no continue in Lua — to skip an item, invert the test and wrap the rest of the body in an if .
Two small gaps here: complete the numeric for's stop value, then add the keyword that exits a loop the moment a match is found. Fill in the blanks and run it.
No blanks this time — just a brief and an outline to keep you on track. FizzBuzz combines a loop, modulo, and the full if / elseif / else ladder, so it's the perfect end-of-lesson test. Build it, run it, and check your output against the example in the comments.
Practice quiz
How does Lua write the "not equal" operator?
- !=
- <>
- ~=
- =/=
Answer: ~=. Lua uses ~= for not-equal; != is a syntax error.
Which values are falsy in Lua?
- 0 and ""
- Only 0
- Only false
- Only nil and false
Answer: Only nil and false. Only nil and false are falsy; 0 and "" are truthy.
Every if block in Lua must be closed with...
- end
- }
- endif
- fi
Answer: end. Lua closes if/for/while/function blocks with the keyword end.
Which keyword exits a loop immediately?
- exit
- break
- stop
- return
Answer: break. break exits the nearest enclosing loop.
Lua's logical operators are written as...
- && || !
- AND OR NOT only uppercase
- and / or / not
- & | ~
Answer: and / or / not. Lua uses the words and, or, and not.
Which loop always runs its body at least once?
- while
- numeric for
- generic for
- repeat ... until
Answer: repeat ... until. repeat...until tests at the bottom, so the body runs at least once.
What does the third number in for i = 1, 10, 2 control?
- The step
- The start
- The stop
- The index base
Answer: The step. The optional third value is the step (here, count by 2).
How do you skip an iteration in Lua (which has no continue)?
- Use the continue keyword
- Wrap the rest of the body in an if
- Call skip()
- Use goto only
Answer: Wrap the rest of the body in an if. Lua has no continue; invert the test and wrap the body in an if.
Which iterator visits a table's keys in order 1, 2, 3...?
- next()
- keys()
- ipairs()
- each()
Answer: ipairs(). ipairs walks integer keys in order, stopping at the first gap.
Does Lua support fuel-- or fuel -= 1 ?
- Both work
- Only fuel--
- Only fuel -= 1
- Neither; write fuel = fuel - 1
Answer: Neither; write fuel = fuel - 1. Lua has no increment or compound-assignment operators.