Counting Up with a Loop
The essential pattern has four parts: initialize the variable, check it with a while condition, use its current value inside the loop, and decrement it before the next repetition.
From Start to Stop
A countdown loop begins with a control variable and repeatedly reduces its value. During each repetition, the program can print or use the current value. The loop continues while its condition is true and stops when that condition is no longer true.
The essential pattern has four parts: initialize the variable, check it with a while condition, use its current value inside the loop, and decrement it before the next repetition.
What do you think happens?
Suppose n starts at 3, the loop continues while n is greater than 0, and the loop prints n before subtracting 1. Which values are printed?
Reveal answer
3, 2, 1
The current value is used before each decrement. The loop prints 3, then changes n to 2; prints 2, then changes n to 1; and prints 1, then changes n to 0. At that point, the condition n > 0 is false.
Tracing Each Iteration
n = 3
while n > 0:
print(n)
n = n - 1- 1The control variable starts at 3.
- 2Because 3 is greater than 0, the loop body runs and prints 3.
- 3The decrement changes n from 3 to 2.
- 4Because 2 is greater than 0, the loop body runs and prints 2.
- 5The decrement changes n from 2 to 1.
- 6Because 1 is greater than 0, the loop body runs and prints 1.
- 7The decrement changes n from 1 to 0.
- 8The next condition check finds that 0 is not greater than 0, so the loop stops.
- n = 3 initializes the control variable.
- while n > 0 checks whether the current value meets the loop condition.
print(n)uses the current value before it changes.- n = n - 1 decrements the control variable before the loop checks the condition again.
3
2
1| Iteration state | Current n | Action |
|---|---|---|
| First check passes | 3 | Print 3, then decrement |
| Second check passes | 2 | Print 2, then decrement |
| Third check passes | 1 | Print 1, then decrement |
| Next check fails | 0 | Stop the loop |
A trace of the control variable for the generated countdown example.
The Control Variable
The control variable is the value that determines whether another repetition may occur. In a countdown loop, the variable moves downward because the loop body contains the update n = n - 1. This update is called decrementation. It is the step that drives the countdown forward and prevents the loop from repeatedly testing the same value.
Finding the Stopping Point
A countdown loop terminates when the control variable reaches a value that no longer satisfies the while condition. In the example with n > 0, the loop body runs for positive values. After the final decrement, n becomes 0, so the next condition check is false and the body is not run again.
Locating the Final Check
Trace a loop that starts with n = 4, continues while n > 1, prints n, and then performs n = n - 1.
- 1Start
n is 4, so the condition n > 1 is true. The loop prints 4 and decrements n to 3.
- 2Continue
n is 3, so the condition is still true. The loop prints 3 and decrements n to 2.
- 3Final body execution
n is 2, so the condition is still true. The loop prints 2 and decrements n to 1.
- 4Terminate
n is now 1. The condition n > 1 is false, so the loop stops without printing 1.
The output is 4, 3, 2. The loop terminates when the control variable reaches 1 because 1 does not satisfy n > 1.
Mistakes to Avoid
Common mistake Forgetting to decrement the control variable
What it looks liken = 3 while n > 0: print(n)Why it failsThe condition can remain true because n is not changed inside the loop.
Do this insteadPlace n = n - 1 inside the loop after the value is used.
Common mistake Using a condition that does not match the intended stopping point
What it looks liken = 3 while n > 1: print(n) n = n - 1Why it failsThis condition stops when n reaches 1, so 1 is not printed.
Do this insteadChoose the while condition according to which values should be included before termination.
Common mistake Putting the decrement outside the loop
What it looks liken = 3 while n > 0: print(n) n = n - 1Why it failsThe update does not occur during each repetition, so it cannot drive the countdown forward.
Do this insteadIndent the decrement so it runs as part of every loop iteration.
Common mistake Decrementing by the wrong amount
What it looks liken = 5 while n > 0: print(n) n = n - 2Why it failsThe control variable changes by two rather than by the one-step decrement used in the basic countdown pattern.
Do this insteadUse n = n - 1 when each successive countdown value should be one less than the previous value.
When checking a countdown loop, trace three things for every iteration: the value before the body runs, the value printed or used, and the value after n = n - 1. Then evaluate the condition once more to locate the exact stopping point.
Practice the Trace
Trace this countdown loop without running it. Write the output values in order and identify the value of n that causes the loop to stop.
n = 5
while n > 2:
print(n)
n = n - 1Hints
- The current value is printed before it is decremented.
- After each printed value, subtract 1 from n.
- The loop stops when n is no longer greater than 2.
Practice Check
Determine the output and stopping value for the practice loop.
- 1First iteration
n is 5, so the condition is true. Print 5, then decrement n to 4.
- 2Second iteration
n is 4, so the condition is true. Print 4, then decrement n to 3.
- 3Third iteration
n is 3, so the condition is true. Print 3, then decrement n to 2.
- 4Condition check
n is 2, and 2 is not greater than 2. The condition is false, so the loop stops.
The output is 5, 4, 3. The stopping value is n = 2.
Key Takeaways
- 1A countdown loop initializes a control variable and checks it with a while condition.
- 2The loop uses the current value before applying the decrement.
- 3The update n = n - 1 moves the control variable toward the stopping condition.
- 4The loop terminates when the condition becomes false.
- 5Forgetting or misplacing the decrement can prevent the countdown from progressing correctly.
At a glance
- A countdown loop repeatedly decreases a control variable while a condition remains true.
- The basic sequence is initialize, check, use the current value, decrement, and check again.
- The statement n = n - 1 is the critical update that advances the countdown.
- Termination occurs after a decrement makes the while condition false.
- Tracing the variable before and after every iteration makes the output and stopping point predictable.