Concepts / Loop Control: Break and Continue

Loop Control: Break and Continue

A countdown loop repeatedly decreases a control variable until a condition is no longer true, printing or using the variable at each step.

  • Programming

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

decrease by 1decrease by 1decrease by 1n = 3prints 3n = 2prints 2n = 1prints 1n = 0condition fails
What value does the control variable have at each iteration, and what output is produced before the next repetition?

The 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 = n - 1n = 3current valuen = 2next value
How does the value change from n to n - 1 after each iteration, and how does that change affect the next condition check?

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.

trueafter workloop backnext repetitionfalsen >= 1check conditionLoop bodyuse nn = n - 1decrease valueCondition checkcheck againLoop endscondition is false
How does the loop move from checking its condition to terminating when the control variable is no longer in range?

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?

Start: The control variable begins at 4, which meets the threshold of 2.

First repetitions: The loop uses 4, then decreases the variable to 3. It then uses 3 and decreases the variable to 2.

Threshold value: The loop uses 2 because 2 still meets the condition n >= 2. The decrement then changes n to 1.

Termination: 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.

breakbehavior not specifiedcontinuebehavior not specifiedCountdown loopdecrease until conditionfails
What does the supplied material establish about break and continue compared with the documented countdown mechanism?

Mistakes That Change the Loop

  • Forgetting to decrement the control variable

    The documented countdown pattern requires the control variable to decrease before the next repetition. Without that step, the countdown does not move forward.

    Fix: Place n = n - 1 inside the loop after the work that uses n.

  • Using the wrong condition

    The initial value does not meet the condition, so the loop body is not entered.

    Fix: Choose a condition whose threshold matches the values the countdown should use.

  • Putting the decrement outside the loop

    The decrement is not reached as part of the repeating loop body, so it cannot drive each repetition forward.

    Fix: Keep the decrement inside the loop.

  • Decrementing by the wrong amount

    The control variable no longer follows a one-step countdown, so values can be skipped.

    Fix: Use n = n - 1 when the intended pattern is a one-step countdown.

Practice the Trace

EASY

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 - 1

Hints
  • 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?

  • 5, 4, 3, 2
  • 5, 4, 3, 2, 1
  • 4, 3, 2
  • 5, 3, 1
Reveal answer

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

  1. A countdown loop initializes a control variable, checks it with a while condition, uses its current value, and decrements it.
  2. The statement n = n - 1 changes the control variable to a value one smaller than before.
  3. The loop terminates when the control variable no longer satisfies the while condition.
  4. For reliable tracing, record the value before each iteration, the output produced, the decrement, and the next condition result.
  5. Forgetting, misplacing, or changing the decrement can prevent the countdown from following the intended pattern.

Key Takeaways

  • 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.