Module 5 · Repeating work with loops
repeat ... until
Run a body once before checking whether it should stop.
What you will learn
- Explain the post-check behavior of repeat until
- Contrast it with while
- post-check loop
- A loop that checks its stopping condition after the body.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A Coin Quest status routine may need to report the current state once even if its completion condition is already met.
The mental model
repeat begins the body immediately. until checks the stopping condition after that body finishes.
The loop continues while the until condition is false and stops when it becomes true.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
local is read as “local”: it creates a name that is available only in the current code scope.
+= is read as “plus equals”: it reads the current value, adds, then stores the result back.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal attempts = 0repeat attempts += 1 print("Attempt", attempts)until attempts >= 2Line by line
- 3
The tracked state starts at zero.
- 5
repeat starts a body without a preceding condition.
- 6
The state progresses before the check.
- 7
Each pass reports its number.
- 8
until stops once attempts is at least two.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Begin at the first executable statement. Comments and blank lines do not run; a branch chooses which block runs, and a loop may revisit its body.
Line 3: The tracked state starts at zero.
Line 5: repeat starts a body without a preceding condition.
Line 6: The state progresses before the check.
Line 7: Each pass reports its number.
Line 8: until stops once attempts is at least two.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: You can explain why the first pass occurs before any stop check.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
Which attempt numbers print?
Try it
Mark each step after you have tried it.
Common mistake
Reading until as 'continue while this is true' reverses its meaning. The condition describes when to stop.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- repeat runs before checking.
- until names the stopping condition.
- The body always executes at least once.
Next: Next, you will leave or skip parts of a loop deliberately.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.