Lua Control Flow
Control flow statements in the Lua programming language are set through the program by defining one or more conditional statements. Specified program code is executed when the condition is true, and other specified code is executed when the condition is false.
Here is a typical flowchart of control flow:
The conditional expression of the control structure can be any value. Lua considers false and nil as false, and true and non-nil as true.
It's important to note that in Lua, 0 is considered true:
Example
--[ 0 is true ]
if(0)
then
print("0 is true")
end
The output of the above code is:
0 is true
Lua provides the following control structures:
Statement | Description |
---|---|
if statement | An if statement consists of a boolean expression followed by one or more statements. |
if...else statement | An if statement can be paired with an else statement, which executes when the if condition expression is false. |
nested if statements | You can use one or more if or else if statements within an if or else if statement. |