Lua Flow Control
The flow control statements of the Lua programming language are defined by setting one or more conditional statements in the program. When the condition is true, the specified program code is executed; when the condition is false, other specified code is executed.
The following is a typical flow control flowchart:
The result of a conditional expression in a control structure can be any value. Lua considers false and nil as false, and true and non-nil as true.
Note that in Lua, 0 is true:
Examples
--[ 0 is true ]
if(0)
then
print("0 is true")
end
if(0)
then
print("0 is true")
end
The output of the above code is:
0 为 true
Lua provides the following control structure statements:
| Statement | Description |
|---|---|
| if statement | if statementConsists of a boolean expression used as the condition, followed by other statements. |
| if...else statement | if statementCan be used withelse statementCombined, the else statement code is executed when the if condition expression is false. |
| Nested if statements | You caniforelse ifuse one or moreiforelse ifstatements. |