English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Lua if…else statements

Lua Flow Control

if...else statements

Lua if statements can be used with else statements, and the else statement code block will be executed when the if condition expression is false.

The syntax format of Lua if...else statements is as follows:

if(BOOLEAN_EXPRESSION)
then
   --[Execute this statement block when the boolean expression is true] --]
else
   --[Execute this statement block when the boolean expression is false] --]
end

When the boolean expression is true, the code block in the if statement will be executed. When the boolean expression is false, the code block in the else statement will be executed.

Lua considers false and nil as false, true and non-nil as true. It is important to note that 0 is considered true in Lua.

The flow chart of if statements is as follows:

Online Example

The following example is used to judge the value of variable a:

--[Define variable --]
a = 100;
--[Check condition --]
if(a < 2"0")
then
   --[Execute this statement block when the condition of if is true --]
   print("a is less than 2"0")
else
   --[Execute this statement block when the condition of if is false --]
   print("a is greater than 2"0")
end
print("The value of a is:", a)

The execution result of the above code is as follows:

a is greater than 20
The value of a is :    100

if...elseif...else statements

Lua if statements can be used with elseif...else statements, which execute the elseif...else statement block when the if condition expression is false, used to detect multiple condition statements.

The syntax format of Lua if...elseif...else statements is as follows:

if( boolean expression 1)
then
   --[In the boolean expression 1 Execute this statement block when it is true --]
elseif( boolean expression 2)
then
   --[In the boolean expression 2 Execute this statement block when it is true --]
elseif( boolean expression 3)
then
   --[In the boolean expression 3 Execute this statement block when it is true --]
else 
   --[Execute this statement block when all the above boolean expressions are not true --]
end

Online Example

The following example judges the value of variable a:

--[Define variable --]
a = 100
--[Check boolean condition --]
if(a == 10 )
then
   --[Print the following information when the condition is true --]
   print("The value of a is ") 10"
elseif(a == 2"0")
then   
   --[Print the following information when the condition of if...else...if is true --]
   print("The value of a is ") 2"0")
elseif(a == 3"0")
then
   --[Print the following information when the condition of if...else...if is true --]
   print("The value of a is ") 3"0")
else
   --[Print the following information when none of the above conditional statements is true --]
   print("No matching value for a")
end
print("The actual value of a is: ", a)

The execution result of the above code is as follows:

No matching value for a
The actual value of a is:     100

Lua Flow Control