Lua if statement

Lua 流程控制Lua Flow Control

Lua if statementIt consists of a boolean expression used as a conditional check, followed by other statements.

The syntax format of the Lua if statement is as follows:
if(布尔表达式)
then
   --[ 在布尔表达式为 true 时执行的语句 --]
end

When the boolean expression is true, the code block inside the if will be executed; when the boolean expression is false, the code immediately following the end of the if statement will be executed.

Lua considers false and nil as false, and true and non-nil as true. Note that in Lua, 0 is true.

The flow chart of the if statement is as follows:

Example

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

Example

--[ Define variables --]
a = 10;

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

The execution result of the above code is as follows:

a 小于 20
a 的值为:    10

Lua 流程控制Lua Flow Control

Other extensions