Module 5 · Repeating work with loops
Safe while loops
Repeat while a condition is true and guarantee that the condition can change.
What you will learn
- Write a progressing while loop
- Explain why a non-yielding infinite loop is dangerous
- while loop
- A loop that checks its condition before each iteration.
- infinite loop
- A loop whose stopping condition is never reached.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Some Coin Quest tasks depend on state rather than a known count, but a loop that never progresses can freeze the script's work.
The mental model
A while loop asks its condition before every pass. Something in or around the body must eventually make that condition false.
If repeated work spans time, yield with an appropriate task API. Do not create a tight endless loop that monopolizes execution.
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 “minus equals”: it reads the current value, subtracts, then stores the result back.
end is read as “end”: it closes a block such as a decision, loop, or function.
while is read as “while”: it repeats while its condition remains truthy.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal coinsRemaining = 3while coinsRemaining > 0 do print("Coins left", coinsRemaining) coinsRemaining -= 1endLine by line
- 3
The state begins above the stopping boundary.
- 5
The condition is checked before each pass.
- 6
The current state is observed.
- 7
This update guarantees progress toward zero.
- 8
end returns control to the next condition check.
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 state begins above the stopping boundary.
Line 5: The condition is checked before each pass.
Line 6: The current state is observed.
Line 7: This update guarantees progress toward zero.
Line 8: end returns control to the next condition check.
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: The loop reaches a false condition after a finite number of iterations.
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
What is coinsRemaining after the loop finishes?
Try it
Mark each step after you have tried it.
Common mistake
while coinsRemaining > 0 do print(coinsRemaining)endThe body never changes coinsRemaining, so the condition stays true. Add progress or choose a different event-driven design.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- while checks before the body.
- Loop state must make progress.
- Avoid non-yielding endless work.
Next: Next, you will use a loop whose body always runs at least once.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.