Easy Tutorial
❮ Lua Variables Lua Repeat Until Loop ❯

Nested if Statements in Lua

Lua Flow Control


if...else Statement

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

Syntax for Nested if Statements in Lua:

if( boolean expression 1)
then
   --[ Executes this block if boolean expression 1 is true --]
   if(boolean expression 2)
   then
      --[ Executes this block if boolean expression 2 is true --]
   end
end

You can nest else if...else statements in the same way.

Example

The following example is used to check 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 if the if condition is true --]
   if( b == 200 )
   then
      --[ Executes this block if the if condition is true --]
      print("Value of a is 100 and b is 200");
   end
end
print("Value of a is :", a );
print("Value of b is :", b );

The above code will produce the following output:

Value of a is 100 and b is 200
Value of a is : 100
Value of b is : 200

Lua Flow Control

❮ Lua Variables Lua Repeat Until Loop ❯