Conditionals in Lua
Conditional statements allow your program to make decisions and
execute
different code based on conditions. They're essential for creating dynamic,
responsive
programs. Lua provides if, elseif, and
else
statements for controlling program flow. Let's learn how to use them
effectively!
The if Statement
The simplest conditional statement checks if a condition is true:
local age = 18
if age >= 18 then
print("You are an adult")
end
- Start with
iffollowed by a condition - Use
thenafter the condition - Write the code to execute if true
- End with
end
Click Run to execute your code
The if-else Statement
Use else to execute code when the condition is false:
local temperature = 25
if temperature > 30 then
print("It's hot!")
else
print("It's not too hot")
end
Click Run to execute your code
The if-elseif-else Statement
Use elseif to check multiple conditions:
local score = 85
if score >= 90 then
print("Grade: A")
elseif score >= 80 then
print("Grade: B")
elseif score >= 70 then
print("Grade: C")
elseif score >= 60 then
print("Grade: D")
else
print("Grade: F")
end
Click Run to execute your code
Truthiness in Lua
In Lua, only false and nil are considered false.
Everything
else is true!
| Value | Truthiness |
|---|---|
false |
โ False |
nil |
โ False |
true |
โ True |
0 |
โ True |
"" (empty string) |
โ True |
{} (empty table) |
โ True |
0 and empty
strings are
true in Lua! Only false and nil are
false.
Click Run to execute your code
Nested Conditionals
You can nest if statements inside each other:
local age = 25
local hasLicense = true
if age >= 18 then
if hasLicense then
print("You can drive")
else
print("You need a license")
end
else
print("You're too young to drive")
end
and, or) to combine conditions instead:
if age >= 18 and hasLicense then
print("You can drive")
end
Combining Conditions
Use logical operators to create complex conditions:
AND Operator
local age = 25
local hasTicket = true
if age >= 18 and hasTicket then
print("You can enter the concert")
end
OR Operator
local isWeekend = true
local isHoliday = false
if isWeekend or isHoliday then
print("No work today!")
end
NOT Operator
local isRaining = false
if not isRaining then
print("Let's go outside!")
end
Click Run to execute your code
Ternary-like Pattern
Lua doesn't have a ternary operator, but you can use
and/or:
-- Pattern: condition and trueValue or falseValue
local age = 20
local status = age >= 18 and "adult" or "minor"
print(status) -- "adult"
-- Traditional if-else equivalent:
local status
if age >= 18 then
status = "adult"
else
status = "minor"
end
and/or pattern only
works if
the true value is not false or nil. When in doubt, use
a
regular if statement!
Common Patterns
Checking for nil
local value = getUserInput()
if value then
print("Got value:", value)
else
print("No value provided")
end
Default Values
local function greet(name)
name = name or "Guest" -- Default to "Guest" if nil
print("Hello, " .. name)
end
greet() -- Hello, Guest
greet("Alice") -- Hello, Alice
Range Checking
local score = 85
if score >= 0 and score <= 100 then
print("Valid score")
else
print("Invalid score")
end
Practice Exercise
Try these conditional challenges:
Click Run to execute your code
Summary
In this lesson, you learned:
- Basic
ifstatements withthenandend if-elsefor two-way decisionsif-elseif-elsefor multiple conditions- Truthiness: only
falseandnilare false - Combining conditions with
and,or,not - Ternary-like pattern with
and/or - Common patterns for nil checking and default values
What's Next?
Now that you can make decisions in your code, it's time to learn about
loops! In the next lesson, we'll explore how to repeat code
with
while, repeat, and for loops. Let's
continue! ๐
Enjoying these tutorials?