Concepts / Counting Up with a Loop

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.

  • Programming

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.

Key point

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?

  • 3, 2, 1

  • 2, 1, 0

  • 3, 2, 1, 0

  • 3, 4, 5

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

Code walkthrough
n = 3
while n > 0:
    print(n)
    n = n - 1
  1. 1
    The control variable starts at 3.
  2. 2
    Because 3 is greater than 0, the loop body runs and prints 3.
  3. 3
    The decrement changes n from 3 to 2.
  4. 4
    Because 2 is greater than 0, the loop body runs and prints 2.
  5. 5
    The decrement changes n from 2 to 1.
  6. 6
    Because 1 is greater than 0, the loop body runs and prints 1.
  7. 7
    The decrement changes n from 1 to 0.
  8. 8
    The 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.
Output
3
2
1
Iteration 1n = 3; print 3Iteration 2n = 2; print 2Iteration 3n = 1; print 1Iteration 4n = 0; condition falsedecrementdecrementdecrement
What value does the control variable have at each iteration, and what does the loop print before the next decrement?
Iteration stateCurrent nAction
First check passes3Print 3, then decrement
Second check passes2Print 2, then decrement
Third check passes1Print 1, then decrement
Next check fails0Stop 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.

n3n2n = n - 1
How does n = n - 1 change the control variable from one iteration to the next?
Check conditionn > 0Use nprint or use current valueDecrement nn = n - 1Stopcondition is falsetruethenloop backfalse
What happens first in a while countdown loop: checking the condition, using the current value, or decrementing the variable?

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.

n = 1n > 0 is truen = 0n > 0 is falsen = n - 1
After which decrement does the condition become false, and why does the loop stop at that point?
3printed first2printed second1printed thirdnext iterationnext iteration
Which sequence of values appears in the output as the loop runs from its starting value toward termination?
Worked example

Locating the Final Check

Trace a loop that starts with n = 4, continues while n > 1, prints n, and then performs n = n - 1.

  1. 1
    Start

    n is 4, so the condition n > 1 is true. The loop prints 4 and decrements n to 3.

  2. 2
    Continue

    n is 3, so the condition is still true. The loop prints 3 and decrements n to 2.

  3. 3
    Final body execution

    n is 2, so the condition is still true. The loop prints 2 and decrements n to 1.

  4. 4
    Terminate

    n is now 1. The condition n > 1 is false, so the loop stops without printing 1.

Result

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 like
    n = 3
    while n > 0:
        print(n)
    Why it fails

    The condition can remain true because n is not changed inside the loop.

    Do this instead

    Place 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 like
    n = 3
    while n > 1:
        print(n)
        n = n - 1
    Why it fails

    This condition stops when n reaches 1, so 1 is not printed.

    Do this instead

    Choose the while condition according to which values should be included before termination.

  • Common mistake

    Putting the decrement outside the loop

    What it looks like
    n = 3
    while n > 0:
        print(n)
    n = n - 1
    Why it fails

    The update does not occur during each repetition, so it cannot drive the countdown forward.

    Do this instead

    Indent the decrement so it runs as part of every loop iteration.

  • Common mistake

    Decrementing by the wrong amount

    What it looks like
    n = 5
    while n > 0:
        print(n)
        n = n - 2
    Why it fails

    The control variable changes by two rather than by the one-step decrement used in the basic countdown pattern.

    Do this instead

    Use n = n - 1 when each successive countdown value should be one less than the previous value.

Best practice

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

MEDIUM

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.
Worked example

Practice Check

Determine the output and stopping value for the practice loop.

  1. 1
    First iteration

    n is 5, so the condition is true. Print 5, then decrement n to 4.

  2. 2
    Second iteration

    n is 4, so the condition is true. Print 4, then decrement n to 3.

  3. 3
    Third iteration

    n is 3, so the condition is true. Print 3, then decrement n to 2.

  4. 4
    Condition check

    n is 2, and 2 is not greater than 2. The condition is false, so the loop stops.

Result

The output is 5, 4, 3. The stopping value is n = 2.

Key Takeaways

  1. 1
    A countdown loop initializes a control variable and checks it with a while condition.
  2. 2
    The loop uses the current value before applying the decrement.
  3. 3
    The update n = n - 1 moves the control variable toward the stopping condition.
  4. 4
    The loop terminates when the condition becomes false.
  5. 5
    Forgetting 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.

Related concepts

More concepts starting with C