Web Analytics

Conditionals in Lua

Beginner ~20 min read

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
Syntax:
  • Start with if followed by a condition
  • Use then after the condition
  • Write the code to execute if true
  • End with end
Output
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
Output
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
Tip: Lua checks conditions from top to bottom and executes the first one that's true. Once a condition matches, the rest are skipped!
Output
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
Important: Unlike many languages, 0 and empty strings are true in Lua! Only false and nil are false.
Output
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
Best Practice: Avoid deep nesting. Use logical operators (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
Output
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
Warning: The 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:

Output
Click Run to execute your code

Summary

In this lesson, you learned:

  • Basic if statements with then and end
  • if-else for two-way decisions
  • if-elseif-else for multiple conditions
  • Truthiness: only false and nil are 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! ๐Ÿš€