Lua Control Structures: if, while, repeat, for, break and goto
Lua provides familiar conditional and loop constructs with a few important semantic details. Only false and nil are false; zero and the empty string are true.
Conditions
and and or return operands rather than forced booleans. The common value or fallback idiom therefore replaces false as well as nil.
Loops
A repeat body runs at least once, and variables declared in the body remain visible in its until condition.
Numeric for evaluates its bounds and step once:
The loop variable is local to the loop and should not be reassigned. Generic for uses an iterator:
break exits the innermost loop. return exits a function and must be the last statement in its block unless wrapped in a nested do ... end. Lua also supports restricted goto labels, which can simplify cleanup or continue-like flow but cannot jump into the scope of a local variable.
Prefer straightforward branches and small functions. A control structure is clearest when loop termination, mutation, and exceptional exits are visible without tracing distant state.