Loop Control: Break and Continue
The loop needs four connected parts: an initial value, a while condition, work performed during the loop, and a decrement before the next repetition.
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…
A Countdown in Motion
A countdown loop repeats an action while a control variable still meets a condition. During each repetition, the loop uses the current value and then decreases that value. The loop eventually stops when the condition is no longer true.
The loop needs four connected parts: an initial value, a while condition, work performed during the loop, and a decrement before the next repetition.
Tracing Each Iteration
n = 3
while n >= 1:
print(n)
n = n - 1- 1The variable n is initialized to 3.
- 2The condition n >= 1 is true, so the loop body runs and prints 3.
- 3The statement n = n - 1 changes n from 3 to 2.
- 4The condition is checked again. It is true for n equal to 2, so 2 is printed.
- 5The decrement changes n from 2 to 1.
- 6The condition is checked again. It is true for n equal to 1, so 1 is printed.
- 7The decrement changes n from 1 to 0.
- 8The next condition check fails because 0 is not at least 1, so the loop terminates.
- n starts at 3
- The while condition checks whether n is at least 1
- The current value is printed
- n is decreased by 1 before the next condition check
3
2
1The Control Variable
The control variable is the value that determines whether another loop iteration can begin. In a countdown loop, it starts at an initial value, is checked by the while condition, is used inside the loop, and is then decreased. The expression n = n - 1 means that the new value of n is one less than its previous value.
n = 3n has its current countdown value
n = n - 1n has a value one smaller than before
Where the Loop Stops
A while loop checks its condition before each repetition. For a countdown that begins at 3 and uses the condition n >= 1, the body runs for 3, 2, and 1. After the final body execution, the decrement changes n to 0. The next check fails because 0 does not meet the threshold of 1.
Finding the Final Condition Check
A countdown starts with n equal to 4, uses the condition n >= 2, and decreases n by 1 after each iteration. Which values are used by the loop, and why does it stop?
- 1Start
The control variable begins at 4, which meets the threshold of 2.
- 2First repetitions
The loop uses 4, then decreases the variable to 3. It then uses 3 and decreases the variable to 2.
- 3Threshold value
The loop uses 2 because 2 still meets the condition n >= 2. The decrement then changes n to 1.
- 4Termination
The next condition check fails because 1 is less than 2.
The loop uses 4, 3, and 2, then terminates when the control variable becomes 1.
Break and Continue Coverage
The supplied material identifies the topic as Loop Control: Break and Continue, but it specifically defines and explains the countdown-loop pattern. It does not provide definitions or control-flow rules for break or continue. This article therefore uses the documented countdown mechanism rather than assigning unsupported behavior to those two constructs.
Mistakes That Change the Loop
Common mistake Forgetting to decrement the control variable
What it looks liken = 3 while n >= 1: print(n)Why it failsThe documented countdown pattern requires the control variable to decrease before the next repetition. Without that step, the countdown does not move forward.
Do this insteadPlace n = n - 1 inside the loop after the work that uses n.
Common mistake Using the wrong condition
What it looks liken = 3 while n > 3: print(n) n = n - 1Why it failsThe initial value does not meet the condition, so the loop body is not entered.
Do this insteadChoose a condition whose threshold matches the values the countdown should use.
Common mistake Putting the decrement outside the loop
What it looks liken = 3 while n >= 1: print(n) n = n - 1Why it failsThe decrement is not reached as part of the repeating loop body, so it cannot drive each repetition forward.
Do this insteadKeep the decrement inside the loop.
Common mistake Decrementing by the wrong amount
What it looks liken = 3 while n >= 1: print(n) n = n - 2Why it failsThe control variable no longer follows a one-step countdown, so values can be skipped.
Do this insteadUse n = n - 1 when the intended pattern is a one-step countdown.
Practice the Trace
Trace this countdown without running it. Write the values printed and identify the value of n at the condition check that ends the loop.
n = 5
while n >= 2:
print(n)
n = n - 1Hints
- Begin with n equal to 5.
- After each printed value, subtract 1.
- The loop ends at the first check where n is less than 2.
What do you think happens?
What values are printed before the loop terminates?
Reveal answer
5, 4, 3, 2
The loop prints each value that meets n >= 2. After printing 2, the decrement changes n to 1, and the next condition check fails.
Key Takeaways
- 1A countdown loop initializes a control variable, checks it with a while condition, uses its current value, and decrements it.
- 2The statement n = n - 1 changes the control variable to a value one smaller than before.
- 3The loop terminates when the control variable no longer satisfies the while condition.
- 4For reliable tracing, record the value before each iteration, the output produced, the decrement, and the next condition result.
- 5Forgetting, misplacing, or changing the decrement can prevent the countdown from following the intended pattern.
At a glance
- A countdown loop repeatedly decreases a control variable while a condition remains true.
- The essential sequence is initialization, condition check, loop work, decrement, and another condition check.
- Decrementation with n = n - 1 drives the loop toward termination.
- A countdown stops when the control variable no longer meets its threshold.
- The supplied source identifies break and continue in the topic title but does not define their behavior.