Lua if nested statement


Release date:2023-09-29 Update date:2023-10-13 Editor:admin View counts:364

Label:

Lua if nested statement

If…else statement

Lua if statement allows nesting, which means that you can use a if or else if statement to insert other if or else if statement.

The syntax format of Lua if nested statements is as follows:

if(Boolean expression 1)
then
   --[Execute the statement block when Boolean expression 1 is true--]
   if(Boolean expression 2)
   then
      --[Execute the statement block when Boolean expression 2 is true--]
   end
end

You can nest in the same way else statement.

Example

The following example is used to determine a variable a and b Value:

Example

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

--[Check Condition--]
if( a == 100 )
then
   --[Perform the following if condition judgment when the if condition is true--]
   if( b == 200 )
   then
      --[Execute the 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:

The value of a is 100, and the value of b is 200
The value of a is: 100
The value of b is: 200

Powered by TorCMS (https://github.com/bukun/TorCMS).