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 conditionNotice 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
EasyYou are creating a simple password validation simulation.
- Create a variable called
passwordand set it to an empty string (""). - Create another variable called
correctPasswordand set it to"secure123". - Create a table called
attemptsthat contains the following password attempts in order:"password""123456""admin""secure123"
- Create a variable called
indexand set it to1. You will use this to track which attempt you are currently on. - Use a
repeat-untilloop to:- Assign the current attempt to
passwordusingattempts[index]— this reads the item at positionindexfrom the table - Print the current
password - Increase
indexby 1 (e.g.index = index + 1) to move to the next attempt
- Assign the current attempt to
- The loop should stop only when
passwordequalscorrectPassword.
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 conditionExample - 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 ofwhile)
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 correctPasswordThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator12Loops Basics
The while LoopThe repeat-until LoopThe Numeric for Loopfor Loop with a StepThe 'break' StatementRecap - Countdown