Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Lua Flow Control

Lua Flow Control

Lua programming language flow control statements are set by the program through one or more conditional statements. When a condition is true, the specified 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 control structure's conditional expression can be any value; Lua considers false and nil as false, and true and non-nil as true.

Note that 0 is true in Lua:

Example

--[ 0 is true ]

if

(

0

)

then

   

print

(

"0 is true"

)

end

The output of the code above is:

0 为 true

Lua provides the following control structure statements:

Statement Description
if statement if statement It consists of a boolean expression as the condition, followed by other statements.
if...else statement if statement It can be used together with else statementused together, the else statement code is executed when the if conditional expression is false.
nested if statements You canif or else ifuse one or more in if or else if statements.