Latest web development tutorials

Lua if statement

Lua Process Control Lua Process Control

Lua if statementconsistsofa Boolean expression as a condition of judgment, immediately followed by other statements.

Lua if statement syntax is as follows:

if(布尔表达式)
then
   --[ 在布尔表达式为 true 时执行的语句 --]
end

Will be executed when the will is true if the block of code in a Boolean expression to false, the code immediately after the end of the if statement will be executed in a Boolean expression.

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

if statement is a flow chart is as follows:

Examples

The following examples are used to determine the value of the variable a is less than 20:

--[ 定义变量 --]
a = 10;

--[ 使用 if 语句 --]
if( a < 20 )
then
   --[ if 条件为 true 时打印以下信息 --]
   print("a 小于 20" );
end
print("a 的值为:", a);

The above code is executed as follows:

a 小于 20
a 的值为:	10

Lua Process Control Lua Process Control