In Lua, you can control your program's flow using if, elseif, and else statements, along with logical operators like and, or, and not. These tools help your code make decisions based on different conditions, making your programs smarter and more flexible.
In this article, we'll cover the basics of decision-making in Lua, with simple examples and tips to help you write better code.
Lua if Statement
The if statement is the most basic conditional structure in Lua, used to execute a block of code if a condition is true. It is essential for making simple decisions based on a single condition.
Syntax
if condition then
-- code to execute if condition is true
end
if Statement
XML
local age = 20
if age >= 18 then
print("You are eligible to vote.")
end
Output
You are eligible to vote.
Lua if-else Statement
The if-else statement allows your program to execute specific blocks of code based on the evaluation of conditions, enabling dynamic decision-making within your scripts.
Syntax
if condition then
-- code to execute if condition is true
else
-- code to execute if condition is false
end
if-else Statement
XML
local temperature = 25
if temperature > 30 then
print("It's hot outside.")
else
print("The weather is pleasant.")
end
Output
The weather is pleasant.
elseif for Multiple Conditions in Lua
The if-elseif-else ladder provides a structured approach to evaluate multiple conditions sequentially, executing the corresponding block of code for the first condition that evaluates to true.
Syntax
if condition1 then
-- code to execute if condition1 is true
elseif condition2 then
-- code to execute if condition2 is true
else
-- code to execute if none of the above conditions are true
end
if-else if-else Ladder
XML
local score = 85
if score >= 90 then
print("Grade: A")
elseif score >= 80 then
print("Grade: B")
elseif score >= 70 then
print("Grade: C")
else
print("Grade: D")
end
Output
Grade: B
Nested if Statements for Complex Logic
Nested if statements allow you to evaluate multiple conditions in a hierarchical manner, executing code blocks based on complex decision trees.
XML
local age = 25
local has_license = true
if age >= 18 then
if has_license then
print("You can drive a car.")
else
print("You need a driver's license.")
end
else
print("You are too young to drive.")
end
Output
You can drive a car.
Logical Operators in Lua
Logical operators are fundamental tools that enable you to perform boolean operations, facilitating decision-making and control flow within your programs.
- and: Returns true if both conditions are true.​
- or: Returns true if at least one condition is true.​
- not: Reverses the truth value of a condition.
XML
local age = 20
local has_ticket = true
if age >= 18 and has_ticket then
print("You can enter the concert.")
else
print("Access denied.")
end
Output
You can enter the concert.
Best Practices for Writing Conditional Logic in Lua
- Use Local Variables: Always declare variables using local inside conditionals to avoid unintended global variables.
- Avoid Deep Nesting: Too many nested if statements can make your code hard to maintain. Use logical operators or helper functions to simplify conditions.
- Readable Code: Proper indentation and comments can make your decision-making logic easier to follow.