Menu
Coddy logo textTech

The repeat-until Loop

Part of the Fundamentals section of Coddy's Lua journey — lesson 72 of 90.

Lua offers another type of loop called the repeat-until loop, which has a unique characteristic that sets it apart from the while loop you just learned. The key difference is that a repeat-until loop always executes its code block at least once, regardless of the condition.

The syntax looks like this:

repeat
    -- code to repeat
until condition

Notice that the condition is checked at the end, after the code has already run once. The loop continues to repeat until the condition becomes true - which is the opposite of a while loop that continues while the condition is true.

Here's a practical example that simulates rolling a dice until you get a 6:

local roll = 0

repeat
    roll = math.random(1, 6)
    print("Rolled:", roll)
until roll == 6

print("Finally got a 6!")

This loop guarantees that you'll roll the dice at least once, and it keeps rolling until the condition roll == 6 becomes true. This "execute first, check later" behavior makes repeat-until loops perfect for situations where you need something to happen at least once before deciding whether to continue.

challenge icon

Challenge

Easy

You are creating a simple password validation simulation.

  1. Create a variable called password and set it to an empty string ("").
  2. Create another variable called correctPassword and set it to "secure123".
  3. Create a table called attempts that contains the following password attempts in order:
    • "password"
    • "123456"
    • "admin"
    • "secure123"
  4. Create a variable called index and set it to 1. You will use this to track which attempt you are currently on.
  5. Use a repeat-until loop to:
    • Assign the current attempt to password using attempts[index] — this reads the item at position index from the table
    • Print the current password
    • Increase index by 1 (e.g. index = index + 1) to move to the next attempt
  6. The loop should stop only when password equals correctPassword.

Each time the loop runs, it reads one attempt from the table, prints it, then moves on to the next. The loop keeps going until the correct password is found.

Cheat sheet

The repeat-until loop executes code at least once before checking the condition. It continues repeating until the condition becomes true.

Syntax:

repeat
    -- code to repeat
until condition

Example - rolling dice until getting a 6:

local roll = 0

repeat
    roll = math.random(1, 6)
    print("Rolled:", roll)
until roll == 6

print("Finally got a 6!")

Key differences from while loops:

  • Always executes at least once
  • Condition is checked at the end
  • Continues until condition is true (opposite of while)

Try it yourself

-- Initialize variables
local password = ""
local correctPassword = "secure123"

-- List of password attempts
local attempts = {"password", "123456", "admin", "secure123"}
local index = 1

-- TODO:
-- Use a repeat-until loop to:
-- 1. Assign the current attempt to `password`
-- 2. Print the attempted password
-- 3. Move to the next attempt
-- Stop when password matches correctPassword
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals