Loop Control: break and continue Statements
A for loop is a definite loop that processes each item in a known collection exactly once.
Try it: while Loops, break and continue
How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.
How it works
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
- If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.
Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.
Loading the simulation…
What the Source Establishes
The supplied material defines a for loop as a definite loop that processes each item in a known collection exactly once. It explains the iteration variable, the required for and in keywords, indentation, the colon after the for statement, and the difference between for loops and while loops. However, the supplied material names break and continue in the section title without defining what either statement does. This article therefore teaches the documented for-loop foundation and clearly marks the behavior that still requires a source definition.
Definite Loop Structure
A for loop is a definite loop because it works through a known collection. The source describes its processing rule as handling each item in that collection exactly once. This makes the collection the basis for the loop's progression: the loop moves from one item to the next rather than relying on an unknown number of repetitions.
The iteration variable is the variable that holds the current item during an iteration. Its value changes automatically as the loop advances through the collection.
Tracing Collection Progress
Following Three Items
Suppose a for loop processes a known collection containing three items. Trace the iteration variable as the loop advances.
- 1First iteration
The iteration variable holds the first item in the collection.
- 2Second iteration
The iteration variable changes automatically and holds the second item.
- 3Third iteration
The iteration variable changes automatically again and holds the third item.
- 4Completion
Each item in the known collection has been processed exactly once.
The collection determines the sequence of items, while the iteration variable represents the current item at each stage.
What do you think happens?
A for loop is processing a known collection with three items. What should you expect about the number of item-processing stages?
Reveal answer
The loop has a stage for each item in the collection.
The supplied definition describes a for loop as a definite loop that processes each item in a known collection exactly once.
Required For Syntax
The source identifies four structural requirements for a for statement: the keyword for, the keyword in, proper indentation, and a colon after the for statement. These requirements describe the form of the loop, while the iteration variable and collection determine what is processed.
- Use the keyword for.
- Use the keyword in.
- Include a colon after the for statement.
- Indent the statements belonging to the loop.
- Use an iteration variable to represent the current item.
For or While
| Loop type | When it is suitable | What the source emphasizes |
|---|---|---|
| for loop | The collection or number of items to process is known | Processes each item in a known collection exactly once |
| while loop | The number of iterations is unknown | Better when the iteration count is not known in advance |
Choosing a loop based on whether the iteration count is known
The key distinction is knowledge of the iteration count. Choose a for loop when you know the collection size in advance. A while loop is described as more suitable when the number of iterations is unknown. This distinction is separate from the undocumented behavior of break and continue; the supplied material does not explain how either statement changes loop execution.
Mistakes to Avoid
Common mistake Treating the iteration variable as fixed
What it looks likeAssuming that the iteration variable keeps the first item for every stage
Why it failsThe source states that the iteration variable changes automatically with each iteration.
Do this insteadTrace the current item separately at each stage of the loop.
Common mistake Using a for loop when the iteration count is unknown
What it looks likeChoosing a for loop even though the stopping point is not known in advance
Why it failsThe source identifies for loops as ideal when the collection size is known and while loops as better for unknown iteration counts.
Do this insteadBase the choice on whether the number of items or iterations is known.
Common mistake Ignoring the structural requirements of a for statement
What it looks likeLeaving out for, in, the colon, or proper indentation
Why it failsThe source identifies all of these as requirements of the syntax.
Do this insteadCheck for the required keywords, colon, and indentation before tracing the loop.
Common mistake Claiming a precise meaning for break or continue from this source alone
What it looks likeExplaining one of those statements without a definition in the supplied material
Why it failsThe source pack names the statements but does not state their effects on control flow.
Do this insteadConsult a source section that explicitly defines each statement before teaching or applying its behavior.
Practice Check
A collection contains five known items. Explain why a for loop is an appropriate loop choice, describe what the iteration variable represents, and state how many times the collection's items are processed according to the supplied definition. Then identify the additional information you would need before explaining how break or continue affects the loop.
Hints
- Use the definition of a definite loop.
- Remember that the iteration variable holds the current item.
- Separate the documented for-loop behavior from the undocumented break and continue behavior.
Key Takeaways
- 1A for loop is a definite loop over a known collection.
- 2The loop processes each item in that collection exactly once according to the supplied definition.
- 3The iteration variable holds the current item and changes automatically as the loop progresses.
- 4A for loop is preferred when the collection size is known; a while loop is better when the iteration count is unknown.
- 5The supplied source pack does not define the control-flow effects of break or continue, so those effects require additional material.
At a glance
- A for loop processes the items of a known collection exactly once.
- The iteration variable identifies the current item and changes automatically on each iteration.
- The choice between for and while depends on whether the iteration count is known.
- The supplied source pack does not provide enough information to explain the behavior of break and continue statements.