Back to Course HomeLesson 5 of 19
Beginner Level • Part 1 & 2

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:

LUA
if score >= 50 then
    print("You passed")
end

The program checks the condition before deciding what to do.

Conditions and Boolean Logic

Every condition in Lua evaluates to a boolean value:

truefalse

Conditional statements rely on:

  • • Comparison operators
  • • Logical operators
  • • Boolean expressions

Example:

LUA
print(10 > 5)

Output:

Output
true

Basic if Statement

The simplest conditional statement in Lua is if.

Syntax

if condition then
    -- code to execute
end

Example

LUA
local age = 18

if age >= 18 then
    print("You are an adult")
end

The code inside the if block runs only if the condition is true.

Note: 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.

LUA
local score = 40

if score >= 50 then
    print("Pass")
else
    print("Fail")
end

Only one block executes.

Using elseif

Use elseif to check multiple conditions.

LUA
local grade = 75

if grade >= 90 then
    print("A")
elseif grade >= 75 then
    print("B")
elseif grade >= 60 then
    print("C")
else
    print("Fail")
end
Important: Lua checks conditions from top to bottom. First matching condition executes, remaining conditions are skipped. Order matters greatly.

Truthy and Falsy Values

In Lua:

  • • Only false and nil are false
  • • Everything else is true
LUA
if 0 then
    print("This runs")
end
Warning: This behavior surprises many beginners. Unlike other languages, 0 and empty strings "" are true in Lua!

Nested if Statements

You can place if statements inside other if blocks.

LUA
local score = 85

if score >= 50 then
    if score >= 80 then
        print("High score")
    end
end

Nested 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:

andornot

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.

LUA
local age = 20
local hasID = true

if age >= 18 and hasID then
    print("Access granted")
else
    print("Access denied")
end

The or Operator

The or operator requires at least one operand to be true for the result to be true.

LUA
local age = 16
local hasPermission = true

if age >= 18 or hasPermission then
    print("Access granted")
else
    print("Access denied")
end

The not Operator

The not operator reverses the boolean value of a condition.

LUA
local isRaining = false

if not isRaining then
    print("Go for a walk")
end

Short-Circuit Evaluation

Lua uses short-circuit evaluation for logical operators:

  • and stops evaluation if the first condition is false
  • or stops evaluation if the first condition is true
LUA
function checkA()
    print("Checking A")
    return false
end

print(checkA() and checkB())

Output:

Output
Checking A
false

checkB() is never called because checkA() is false.

Ready to test your knowledge?

Take the quiz to verify your understanding of conditional statements.