Lua if Nested Statements

Lua 流程控制Lua Flow Control


if...else statement

Lua allows nested if statements, which means you can insert other if or else if statements within an if or else if statement.

The syntax format for Lua if nested statements is as follows:
if( 布尔表达式 1)
then
   --[ 布尔表达式 1 为 true 时执行该语句块 --]
   if(布尔表达式 2)
   then
      --[ 布尔表达式 2 为 true 时执行该语句块 --]
   end
end

You can nest in the same wayelse if...elsestatement.

Example

The following example is used to determine the values of variables a and b:

Example

--[ Define variables --]
a = 100;
b = 200;

--[ Check conditions --]
if( a == 100 )
then
   --[ Execute the following if condition check when the if condition is true --]
   if( b == 200 )
   then
      --[ Execute this statement block when the if condition is true --]
      print("The value of a is 100 and the value of b is 200" );
   end
end
print("The value of a is:", a );
print("The value of b is:", b );

The execution result of the above code is as follows:

a 的值为 100 b 的值为 200
a 的值为 :    100
b 的值为 :    200

Lua 流程控制Lua Flow Control

Other Extensions