Module 5 · Repeating work with loops
Controlling loops with break and continue
Exit a loop or skip the rest of one iteration.
What you will learn
- Distinguish break from continue
- Trace skipped and terminating iterations
- break
- Exit the nearest loop immediately.
- continue
- Skip the rest of the current iteration and begin the next one.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A search can stop as soon as Coin Quest finds its target, while invalid entries can be skipped without abandoning the whole search.
The mental model
continue moves to the next iteration. break leaves the loop entirely.
Place these controls behind specific conditions so a reader can see why work is skipped or stopped.
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.
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.
== is read as “equals equals”: it compares two values and produces a boolean.
if is read as “if”: it starts a decision by evaluating a condition.
then is read as “then”: it marks the body that can run when the condition is truthy.
end is read as “end”: it closes a block such as a decision, loop, or function.
for is read as “for”: it starts a controlled repetition with a changing loop value.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictfor coinIndex = 1, 6 do if coinIndex == 2 then continue end if coinIndex == 5 then break end print(coinIndex)endLine by line
- 3
The loop visits candidate indices one through six.
- 4
Index two is the skip case.
- 5
continue bypasses the rest of this body.
- 7
Index five is the stop case.
- 8
break exits before printing five.
- 10
Only unskipped values before the break are printed.
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 loop visits candidate indices one through six.
Line 4: Index two is the skip case.
Line 5: continue bypasses the rest of this body.
Line 7: Index five is the stop case.
Line 8: break exits before printing five.
Line 10: Only unskipped values before the break are printed.
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: Your trace identifies which iteration is skipped and where the loop ends.
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 indices print?
Try it
Mark each step after you have tried it.
Common mistake
Using break when you meant continue discards every later candidate. Ask whether the current item is bad or the whole search is finished.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- continue skips one iteration's remaining work.
- break exits the loop.
- Specific conditions should explain either control.
Next: Next, you will iterate through an ordered collection of values.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.