English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Lua Flow Control

Lua programming language flow control statements set a condition statement or multiple condition statements through the program to determine. The specified code is executed when the condition is true, and other specified code is executed when the condition is false.

Here is a typical flow control flow chart:


The result of the condition expression in the control structure can be any value, Lua considers false and nil as false, and true and non-nil as true.

It should be noted that 0 is true in Lua:

--[0 is true]
if(0)
then
    print("0 is true")
end

The output of the above code is as follows:

0 is true

Lua provides the following control structure statements:

StatementDescription
if Statementif Statement consisting of a boolean expression as the condition followed by other statements.
if...else Statementif Statement can be used with else Statementtogether, to execute the else statement code when the if condition expression is false.
Nested if Statements in LuaYou can useif or else ifusing one or more if or else if Statement.