Counting Up with a Loop
A countdown loop repeatedly decreases a control variable until a condition is no longer true, printing or using the variable at each step.
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
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
| 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.
Start: n is 4, so the condition n > 1 is true. The loop prints 4 and decrements n to 3.
Continue: n is 3, so the condition is still true. The loop prints 3 and decrements n to 2.
Final body execution: n is 2, so the condition is still true. The loop prints 2 and decrements n to 1.
Terminate: 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
Forgetting to decrement the control variable
The condition can remain true because n is not changed inside the loop.
Fix:
Place n = n - 1 inside the loop after the value is used.Using a condition that does not match the intended stopping point
This condition stops when n reaches 1, so 1 is not printed.
Fix:
Choose the while condition according to which values should be included before termination.Putting the decrement outside the loop
The update does not occur during each repetition, so it cannot drive the countdown forward.
Fix:
Indent the decrement so it runs as part of every loop iteration.Decrementing by the wrong amount
The control variable changes by two rather than by the one-step decrement used in the basic countdown pattern.
Fix:
Use 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 - 1
Hints
- 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.
First iteration: n is 5, so the condition is true. Print 5, then decrement n to 4.
Second iteration: n is 4, so the condition is true. Print 4, then decrement n to 3.
Third iteration: n is 3, so the condition is true. Print 3, then decrement n to 2.
Condition 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
- A countdown loop initializes a control variable and checks it with a while condition.
- The loop uses the current value before applying the decrement.
- The update n = n - 1 moves the control variable toward the stopping condition.
- The loop terminates when the condition becomes false.
- Forgetting or misplacing the decrement can prevent the countdown from progressing correctly.
Key Takeaways
- 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.