Conditional Statements and Logical Flow in Lua
Lesson Overview
Conditional statements are what allow a Lua program to make decisions. Without conditionals, a program would always run the same way, every time, with no ability to react to user input, changing values, or game states.
This lesson is intentionally written as a very long, multi-part deep dive. Conditionals are one of the most common sources of beginner mistakes, so we will explore them carefully and thoroughly.
Lesson Objectives
By the end of this complete lesson series, you will be able to:
- Understand what conditional statements are
- Write correct if, elseif, and else statements
- Understand how Lua evaluates conditions
- Control the logical flow of a program
- Avoid common conditional logic mistakes
Part 1: Fundamentals of Conditional Statements
Understanding if, else, and elseif statements
What Are Conditional Statements?
A conditional statement allows a program to execute code only if a condition is true.
In simple terms:
- • If something is true → do this
- • Otherwise → do something else
Example:
if score >= 50 then
print("You passed")
endThe program checks the condition before deciding what to do.
Conditions and Boolean Logic
Every condition in Lua evaluates to a boolean value:
Conditional statements rely on:
- • Comparison operators
- • Logical operators
- • Boolean expressions
Example:
print(10 > 5)Output:
trueBasic if Statement
The simplest conditional statement in Lua is if.
Syntax
if condition then
-- code to execute
endExample
local age = 18
if age >= 18 then
print("You are an adult")
endThe code inside the if block runs only if the condition is true.
then marks the beginning of the conditional block and end marks the end. Lua uses end instead of braces {}.Using else
The else keyword defines code that runs when the condition is false.
local score = 40
if score >= 50 then
print("Pass")
else
print("Fail")
endOnly one block executes.
Using elseif
Use elseif to check multiple conditions.
local grade = 75
if grade >= 90 then
print("A")
elseif grade >= 75 then
print("B")
elseif grade >= 60 then
print("C")
else
print("Fail")
endTruthy and Falsy Values
In Lua:
- • Only
falseandnilare false - • Everything else is true
if 0 then
print("This runs")
end0 and empty strings "" are true in Lua!Nested if Statements
You can place if statements inside other if blocks.
local score = 85
if score >= 50 then
if score >= 80 then
print("High score")
end
endNested logic should be used carefully to avoid complexity.
Common Beginner Mistakes
- Forgetting then
- Missing end
- Using = instead of ==
- Overusing nested if statements
Part 2: Logical Operators and Complex Conditions
Combining conditions with and, or, and not
Logical Operators in Lua
Lua provides three core logical operators:
These operators are used primarily in if statements, loops, and conditional assignments.
The and Operator
The and operator requires both operands to be true for the result to be true.
local age = 20
local hasID = true
if age >= 18 and hasID then
print("Access granted")
else
print("Access denied")
endThe or Operator
The or operator requires at least one operand to be true for the result to be true.
local age = 16
local hasPermission = true
if age >= 18 or hasPermission then
print("Access granted")
else
print("Access denied")
endThe not Operator
The not operator reverses the boolean value of a condition.
local isRaining = false
if not isRaining then
print("Go for a walk")
endShort-Circuit Evaluation
Lua uses short-circuit evaluation for logical operators:
- •
andstops evaluation if the first condition is false - •
orstops evaluation if the first condition is true
function checkA()
print("Checking A")
return false
end
print(checkA() and checkB())Output:
Checking A
falsecheckB() is never called because checkA() is false.
Ready to test your knowledge?
Take the quiz to verify your understanding of conditional statements.