Control Flow and Loops
Master conditionals, loops, and Lua's unique truthiness rules.
What You'll Learn
- if/elseif/else conditionals
- Lua's unique truthiness: only nil and false are falsy
- Numeric for, while, and repeat...until loops
- Generic for with pairs() and ipairs()
Conditionals
Lua uses if, elseif, and else — note the single word elseif (not else if). Every if block must end with end.
local score = 85
if score >= 90 then
print("Grade: A")
elseif score >= 80 then
print("Grade: B")
elseif score >= 70 then
print("Grade: C")
else
print("Grade: F")
end⚠️ Common Mistake: Truthiness
In Lua, 0 is truthy! Only nil and false are falsy. This trips up developers coming from JavaScript, Python, or C where 0 is falsy.
if 0 then print("This WILL print in Lua!") end
if "" then print("This WILL also print!") end
if nil then print("This will NOT print") endThe Ternary Idiom
Lua has no ternary operator (? :), but you can use and/or:
local role = isAdmin and "Admin" or "User" -- Equivalent to: isAdmin ? "Admin" : "User"
Conditionals & Logic
Test Lua's conditional logic and truthiness rules.
// Lua Conditionals — simulated in JavaScript
console.log("=== if / elseif / else ===");
console.log();
const score = 85;
console.log("Score:", score);
if (score >= 90) {
console.log("Grade: A — Excellent!");
} else if (score >= 80) {
console.log("Grade: B — Great job!");
} else if (score >= 70) {
console.log("Grade: C — Good effort");
} else {
console.log("Grade: F — Keep practicing");
}
console.log();
console.log("=== Logical Operators ===");
console.log("Lua uses: and, or, not (not
...Loops
Lua provides three loop types, plus the generic for with iterators:
1. Numeric for
-- for var = start, stop[, step] do for i = 1, 10 do print(i) -- 1 through 10 end for i = 10, 0, -2 do print(i) -- 10, 8, 6, 4, 2, 0 end
2. while
local fuel = 100
while fuel > 0 do
fuel = fuel - 10
print("Fuel: " .. fuel)
end3. repeat...until
Similar to do...while in other languages — the loop body runs at least once:
repeat local input = io.read() until input == "quit"
💡 Pro Tip
Use break to exit a loop early. Lua does not have continue — instead, use goto (Lua 5.2+) with a label at the end of the loop body.
Loops
Practice all of Lua's loop structures.
// Lua Loops — simulated in JavaScript
console.log("=== Numeric for Loop ===");
console.log("for i = 1, 5 do ... end");
for (let i = 1; i <= 5; i++) {
process.stdout ? null : null;
console.log(" i =", i);
}
console.log();
console.log("=== for with Step ===");
console.log("for i = 10, 0, -2 do ... end");
for (let i = 10; i >= 0; i -= 2) {
console.log(" i =", i);
}
console.log();
console.log("=== while Loop ===");
let fuel = 5;
console.log("while fuel > 0 do ...");
while (fuel > 0) {
c
...📋 Quick Reference
| Concept | Syntax |
|---|---|
| if/else | if x then ... elseif ... else ... end |
| Numeric for | for i = 1, 10 do ... end |
| while | while cond do ... end |
| repeat | repeat ... until cond |
| Logical AND | and |
| Logical NOT | not |
🎉 Lesson Complete!
You now understand Lua's control flow including its unique truthiness rules. Next up: metatables and metamethods — Lua's most powerful feature for OOP and operator overloading.
Sign up for free to track which lessons you've completed and get learning reminders.