In Lua, statements are the building blocks of control flow and logic. They define what your program does, how it executes, and when different blocks of code should run.
This guide covers Lua statements in depth, including expressions, control structures, declarations, and loops, with examples to help you understand each one clearly.
What is a Statement?
A statement is a line or block of code that performs an action, such as assigning a value, looping, or calling a function.
Lua has simple statements (like assignment or function calls) and compound statements (like conditionals or loops).
Expression and Assignment Statements
Assignment
x = 10
y, z = 1, 2
You can assign multiple values at once. Lua uses tuple unpacking.
Undefined variables are implicitly nil
print(unknown) --> nil
Looping Statements
1. while Loop
local i = 1
while i <= 5 do
print(i)
i = i + 1
end
2. repeat...until Loop
Runs the block at least once, then checks the condition.
local i = 1
repeat
print(i)
i = i + 1
until i > 5
3. for Numeric Loop
for i = 1, 5 do
print(i)
end
-- with step
for i = 10, 1, -2 do
print(i)
end
4. for Generic Loop
Used with iterators (e.g., pairs, ipairs):
local t = {a = 1, b = 2}
for k, v in pairs(t) do
print(k, v)
end
Conditional Statements
if, elseif, else
local score = 85
if score >= 90 then
print("Grade: A")
elseif score >= 80 then
print("Grade: B")
else
print("Grade: C or below")
end
Lua treats false and nil as falsey; everything else is truthy (even 0 and empty strings).
goto Statement
Lua supports labeled jumps (from 5.2+).
for i = 1, 5 do
if i == 3 then goto skip end
print(i)
::skip::
end
Use sparingly — it’s powerful but can make code hard to follow.
return Statement
Used to return values from a function.
function add(a, b)
return a + b
end
In top-level scripts (outside functions), return can terminate execution if needed.
break Statement
Used to exit a loop early.
for i = 1, 10 do
if i == 5 then break end
print(i)
end
break is only valid inside loops.
Block Statements
Blocks are sequences of statements grouped by do...end.
do
local x = 42
print(x)
end
-- x is not accessible here
Useful for creating limited scopes.
Function Declarations (as Statements)
function greet(name)
print("Hello, " .. name)
end
Or using anonymous function syntax:
greet = function(name)
print("Hi, " .. name)
end
Local Declarations
Local Variable
local count = 0
Local Function
local function hello()
print("Hi")
end
Local declarations are scoped to the block they’re defined in.
Full List of Lua Statement Types
| Statement Type | Keyword(s) | Description |
|---|---|---|
| Assignment | = |
Assigns values to variables |
| Function call | (implicit) | Invokes a function |
if statement |
if, elseif, else |
Conditional branching |
| Looping | while, repeat, for |
Executes code repeatedly |
| Block | do...end |
Creates a scope |
| Return | return |
Returns value(s) from a function |
| Break | break |
Exits a loop early |
| Goto | goto, ::label:: |
Jumps to a labeled block |
| Local declaration | local |
Declares local variables or functions |
| Function declaration | function |
Defines a function |
Tips for Writing Clean Lua Statements
- Use
localas much as possible to limit scope. - Avoid deep nesting; use early
returnorbreakto simplify logic. - Structure logic with clear blocks using
do...end. - Use
forloops when iteration bounds are known;whilewhen they are not. - Limit use of
gototo escape deeply nested logic when absolutely necessary.