Courses/Lua/Control Flow and Loops

    Control Flow and Loops

    Master conditionals, loops, and Lua's unique truthiness rules.

    What You'll Learn

    • if/elseif/else conditionals
    • Lua's unique truthiness: only nil and false are falsy
    • Numeric for, while, and repeat...until loops
    • Generic for with pairs() and ipairs()

    Conditionals

    Lua uses if, elseif, and else — note the single word elseif (not else if). Every if block must end with end.

    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: F")
    end

    ⚠️ Common Mistake: Truthiness

    In Lua, 0 is truthy! Only nil and false are falsy. This trips up developers coming from JavaScript, Python, or C where 0 is falsy.

    if 0 then print("This WILL print in Lua!") end
    if "" then print("This WILL also print!") end
    if nil then print("This will NOT print") end

    The Ternary Idiom

    Lua has no ternary operator (? :), but you can use and/or:

    local role = isAdmin and "Admin" or "User"
    -- Equivalent to: isAdmin ? "Admin" : "User"

    Conditionals & Logic

    Test Lua's conditional logic and truthiness rules.

    Try it Yourself »
    JavaScript
    // Lua Conditionals — simulated in JavaScript
    console.log("=== if / elseif / else ===");
    console.log();
    
    const score = 85;
    console.log("Score:", score);
    
    if (score >= 90) {
      console.log("Grade: A — Excellent!");
    } else if (score >= 80) {
      console.log("Grade: B — Great job!");
    } else if (score >= 70) {
      console.log("Grade: C — Good effort");
    } else {
      console.log("Grade: F — Keep practicing");
    }
    console.log();
    
    console.log("=== Logical Operators ===");
    console.log("Lua uses: and, or, not (not
    ...

    Loops

    Lua provides three loop types, plus the generic for with iterators:

    1. Numeric for

    -- for var = start, stop[, step] do
    for i = 1, 10 do
      print(i)            -- 1 through 10
    end
    
    for i = 10, 0, -2 do
      print(i)            -- 10, 8, 6, 4, 2, 0
    end

    2. while

    local fuel = 100
    while fuel > 0 do
      fuel = fuel - 10
      print("Fuel: " .. fuel)
    end

    3. repeat...until

    Similar to do...while in other languages — the loop body runs at least once:

    repeat
      local input = io.read()
    until input == "quit"

    💡 Pro Tip

    Use break to exit a loop early. Lua does not have continue — instead, use goto (Lua 5.2+) with a label at the end of the loop body.

    Loops

    Practice all of Lua's loop structures.

    Try it Yourself »
    JavaScript
    // Lua Loops — simulated in JavaScript
    console.log("=== Numeric for Loop ===");
    console.log("for i = 1, 5 do ... end");
    for (let i = 1; i <= 5; i++) {
      process.stdout ? null : null;
      console.log("  i =", i);
    }
    console.log();
    
    console.log("=== for with Step ===");
    console.log("for i = 10, 0, -2 do ... end");
    for (let i = 10; i >= 0; i -= 2) {
      console.log("  i =", i);
    }
    console.log();
    
    console.log("=== while Loop ===");
    let fuel = 5;
    console.log("while fuel > 0 do ...");
    while (fuel > 0) {
      c
    ...

    📋 Quick Reference

    ConceptSyntax
    if/elseif x then ... elseif ... else ... end
    Numeric forfor i = 1, 10 do ... end
    whilewhile cond do ... end
    repeatrepeat ... until cond
    Logical ANDand
    Logical NOTnot

    🎉 Lesson Complete!

    You now understand Lua's control flow including its unique truthiness rules. Next up: metatables and metamethods — Lua's most powerful feature for OOP and operator overloading.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service