Concepts / Loop Control: Break and Continue

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.

  • Programming
Interactive lab

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

  1. Evaluate the condition. If it is False, skip the body and continue after the loop.
  2. If it is True, run the body from the top, then go back and evaluate the condition again.
  3. continue skips the rest of the body and goes straight back to the condition check.
  4. 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').
  5. 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.

Educational simulation

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.

Key point

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

Code walkthrough
n = 3
while n >= 1:
    print(n)
    n = n - 1
  1. 1
    The variable n is initialized to 3.
  2. 2
    The condition n >= 1 is true, so the loop body runs and prints 3.
  3. 3
    The statement n = n - 1 changes n from 3 to 2.
  4. 4
    The condition is checked again. It is true for n equal to 2, so 2 is printed.
  5. 5
    The decrement changes n from 2 to 1.
  6. 6
    The condition is checked again. It is true for n equal to 1, so 1 is printed.
  7. 7
    The decrement changes n from 1 to 0.
  8. 8
    The 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
Output
3
2
1
n = 3prints 3n = 2prints 2n = 1prints 1n = 0condition failsdecrease by 1decrease by 1decrease by 1
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.

Before
n = 3

n has its current countdown value

After
n = n - 1

n has a value one smaller than before

n = 3current valuen = 2next valuen = n - 1
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.

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

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?

  1. 1
    Start

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

  2. 2
    First repetitions

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

  3. 3
    Threshold value

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

  4. 4
    Termination

    The next condition check fails because 1 is less than 2.

Result

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

  • Common mistake

    Forgetting to decrement the control variable

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

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

    Do this instead

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

  • Common mistake

    Using the wrong condition

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

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

    Do this instead

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

  • Common mistake

    Putting the decrement outside the loop

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

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

    Do this instead

    Keep the decrement inside the loop.

  • Common mistake

    Decrementing by the wrong amount

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

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

    Do this instead

    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. 1
    A countdown loop initializes a control variable, checks it with a while condition, uses its current value, and decrements it.
  2. 2
    The statement n = n - 1 changes the control variable to a value one smaller than before.
  3. 3
    The loop terminates when the control variable no longer satisfies the while condition.
  4. 4
    For reliable tracing, record the value before each iteration, the output produced, the decrement, and the next condition result.
  5. 5
    Forgetting, 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.

Related concepts

More concepts starting with L