If Else Statement In Lua
# Lua if...else Statement
[ Lua Flow Control](#)
* * *
## if...else Statement
A Lua `if` statement can be paired with an `else` statement. The code block in the `else` statement is executed when the `if` condition expression evaluates to `false`.
The syntax for the Lua `if...else` statement is as follows:
if( boolean_expression) then
--
else
--
end
The code block inside the `if` is executed when the boolean expression is `true`, and the code block inside the `else` is executed when the boolean expression is `false`.
In Lua, `false` and `nil` are considered false, while `true` and any non-nil value are considered true. Note that in Lua, `0` is considered `true`.
The flowchart for an `if` statement is as follows:

### Example
The following example checks the value of variable `a`:
## Example
--
a =100;
--
if( a <20)
then
--
print("a is less than 20")
else
--
print("a is greater than 20")
end
print("value of a is :", a)
The output of the above code is:
a is greater than 20
value of a is :100
* * *
## if...elseif...else Statement
A Lua `if` statement can be paired with `elseif...else` statements. The `elseif...else` statement code block is executed when the `if` condition expression is `false`, and is used to check multiple conditional statements.
The syntax for the Lua `if...elseif...else` statement is as follows:
if( boolean_expression 1) then
--
elseif( boolean_expression 2) then
--
elseif( boolean_expression 3) then
--
else
--
end
### Example
The following example checks the value of variable `a`:
## Example
--
a =100
--
if( a ==10)
then
--
print("value of a is 10")
elseif( a ==20)
then
--
print("value of a is 20")
elseif( a ==30)
then
--
print("value of a is 30")
else
--
print("no match for a")
end
print("real value of a is: ", a )
The output of the above code is:
no match for a
real value of a is: 100
[ Lua Flow Control](#)
YouTip