Like many other programming languages, learning how to control the if-else condition is fundamental. If you are new in coding, the if statement is basically telling the program if a block of code should be executed according to a specified condition. Execute it if it’s true, or skip it if it’s false.
With the help of the if-else, you can make better decisions and control the flow of your program. Now, let’s dive into how the if condition works in Lua. And I have prepared tons of examples for you to understand clearly.
You can use Lua Online Editor to try the examples below:
Lua if Condition syntax
The basic syntax of an if
condition in Lua is straightforward.
If the condition evaluates to true
, the code inside the if
block is executed. If the condition is false
, the code inside the if
block is skipped. end if needed at the end.
Syntax of if-then-end
if condition then
-- Code to execute if the condition is true
end
Simple example using if-then-end
local number = 100
if number > 50 then
print("The number is greater than 50!")
end
In the above example, we call “number > 50” is the condition. In this case, the condition equal to true
, so the message “The number is greater than 50!” is printed it out.
Lua if-else Condition
Sometime you would like to execute one block of code if the condition is true and a different block if the condition is false. That’s why you need to know the else
keyword.
if-then-else syntax
if condition then
-- Code to execute if the condition is true
else
-- Code to execute if the condition is false
end
if-then-else example
local number = 30
if number > 50 then
print("The number is greater than 50")
else
print("The number is smaller than 50")
end
So in this case, the condition number > 5
is evaluates to false. Therefore, the message will show “The number is smaller than 50”.
Lua if-elseif-else Condition
In most cases, you need to handle multiple conditions. You need to use elseif
keyword to handle the case. This simply allows you to tackle the multiple conditions together.
if-elseif-else syntax
if condition1 then
-- if condition1 is true
elseif condition2 then
-- if condition2 is true
elseif condition3 then
-- if condition3 is true
else
-- if none of the above conditions are true
end
if-elseif-else example
local number = 70
if number > 100 then
print("The number is greater than 100")
elseif number > 50 then
print("The number is greater than 50 but less than or equal to 100")
else
print("The number is 50 or less")
end
In the above example, the condition number > 100
is false, but number > 50
is true. Therefore, the message “The number is greater than 50 but less than or equal to 100” is printed. Try it out in your Lua compiler! You can modify the conditions to make it even more complicated.
Lua Nested if
Conditions
For complex logic, you can using if-else condition inside another if condition. We call it the nested if
conditions. Remember to handle it carefully. From my experience, most of the time the program bugs occur with careless arrangement of the nested if coding.
Lua Nested if example
local number = 120
if number > 50 then
if number > 100 then
print("The number is greater than 100")
else
print("The number is greater than 50 but less than or equal to 100")
end
else
print("The number is 50 or less")
end
Let me explain a bit for the above example. The outer condition number > 50
is true so the code inside the outer if block will be executed. Within that block, the inner condition number > 100
is also true so the message “The number is greater than 100” will be printed out. This is just a simple example, in real case the situation can be much more complicated!
Combined with Logical Operators
Lua provides three primary logical operators. Named and, or, and not. With these logical operators, you can control the flow of your code with boolean expressions more efficiently.
The and operator returns true if both expressions are true, and false otherwise.
The or
operator returns true
if at least one of the expressions is true.
The not
operator is a unary operator that negates the expression, returning true
if the expression is false, and vice versa.
You can make good use of these operators in decision-making process. Combined with the if statements, you can have a better control of you program. In game development, it’s really so common to use logical operators with if-else condition.
Operator | Expression | Result |
and | true and true | true |
true and false | false | |
false and false | false | |
or | true or false | true |
false or false | false | |
not | not true | false |
not false | true |
Lua logical condition example
local number = 8
if number > 5 and number < 10 then
print("The number is between 5 and 10")
end
From the above, the combined condition number > 5 and number < 10
evaluates to true so the message “The number is between 5 and 10” is printed.
Conclusion
Once you master how to use the if
condition in Lua, you can have a full control of the flow of your program. No matter it’s a dynamic conditions or simple if-then-else, you can handle it really well. By correct use of if
, else
, and elseif
statements, as well as nesting and logical operators, you can handle a wide range of decision-making scenarios in your code.
In the next chapter, we will go through another important concept “LOOP” in Lua. Before that, please try your best to practice 🙂