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

Lua if statement

Lua Flow Control

Lua If statement It consists of a boolean expression as a condition for judgment, followed by other statements.

The syntax format of Lua if statement is as follows:

if(BOOLEAN_EXPRESSION)
then
   --[The statement executed when the boolean expression is true --]
end

When the boolean expression is true, the code block in the if statement will be executed, and when the boolean expression is false, the code following the if statement end will be executed.

Lua considers false and nil as false, and true and non-nil as true. It should be noted that 0 is true in Lua.

The flow chart of the if statement is as follows:

Online Example

The following example is used to determine whether the value of variable a is less than 20:

--[Define a variable --]
a = 10;
--[Use the if statement --]
if(a < 20)
then
   --[Print the following information if the condition is true --]
   print("a is less than 2"0");
end
print("The value of a is:", a);

The following is the execution result of the above code:

a is less than 20
The value of a is:    10

Lua Flow Control