Concepts / The break and continue Statements

The break and continue Statements

while True keeps entering the loop because its condition is always true. The loop stops only when execution reaches a break statement inside it.

  • 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 Loop That Waits

Many interactive programs need to keep asking for input until the user signals that the interaction is finished. The loop must repeat for an unknown number of iterations, so the program cannot rely on a fixed count. Python's while True pattern provides the repetition, while break provides the stopping point.

Key point

while True keeps entering the loop because its condition is always true. The loop stops only when execution reaches a break statement inside it.

while Truecondition is trueLoop bodyprompt and process inputNext iterationask againCode after loopreached by breakenterno breakrepeatbreak
How does the loop continue running when no stopping condition appears in the while statement, and where can execution leave it?

Tracing Each Input

python

This generated code follows the interactive loop described in the source. The loop begins with while True. Each iteration asks for input and stores the response in line. If line is exactly done, break runs immediately. Otherwise, the program prints the entered text and returns to the top of the loop. After break exits the loop, the final print displays Done!.

Inputstore response in lineline == donecompare commandbreakexit loopNext promptrepeat loopPrint lineprocess inputDone!code after loopresponse receivedyesnoafter printrepeatexit loop
What happens after each user input, and how does control flow either repeat the loop or exit it?
Worked example

Inputs: apple, banana, done

Trace the loop when the user enters apple, then banana, then done.

  1. 1
    First iteration

    The user enters apple. The condition line == done is false, so break is skipped. The program prints apple and returns to the top of the loop.

  2. 2
    Second iteration

    The user enters banana. The condition is again false, so the program prints banana and starts another iteration.

  3. 3
    Third iteration

    The user enters done. The condition is true, so break immediately exits the loop. The remaining code in that iteration is not executed.

  4. 4
    After the loop

    Execution continues with the statement after the loop, which prints Done!.

Result

The loop prints apple, prints banana, stops when done is entered, and then prints Done!.

What do you think happens?

What happens when the user enters done as the first input?

  • The program prints done and asks for another input

  • The loop exits immediately and the code after the loop runs

  • The loop condition becomes false before the input is checked

Reveal answer
Answer:

The loop exits immediately and the code after the loop runs.

The input is checked before the print statement. When the condition matches done, break immediately exits the loop, so the remaining code in that iteration is skipped.

How break Controls Exit

break is a loop-control statement. When Python encounters it, execution leaves the loop immediately, regardless of how many iterations might otherwise remain. Python does not execute the remaining statements in the current iteration. Execution resumes with the code after the loop.

doneuser inputExit conditionmatch foundbreakleave loop nowDone!statement after loopchecktrueexit
When the user enters the exit command, how does break move execution from inside the loop to the code after the loop?
StatementEffect in the loopWhat happens next
breakExits the loop immediatelyExecution continues after the loop
continueSkips the remaining code in the current iterationThe loop begins its next iteration

Both statements change normal loop execution, but only break exits the loop.

breakexit loopCode after loopexecution continuescontinueskip remaining bodyNext iterationloop repeatsleave loopstart again
Does the statement stop the entire loop, or does it skip the remaining body and begin the next iteration?

Commands Through the Loop

An interactive input loop can be understood as a repeated command-handling sequence: prompt the user, receive input, check whether it is the stop command, and then either exit or process the input. A non-stop command completes the current iteration and leads back to the prompt. The stop command takes a different path and reaches break.

Userenters commandinputstores lineCondition checkcompare with doneResponseprint or breakNext promptif loop continuescommandlineevaluatenon-stop input
How does a command entered by the user move through input, condition checking, response handling, and the next iteration?

The important trace question is not only what value the user entered, but which branch that value selects. For apple or banana, the condition is not met, so the input is processed and the loop repeats. For done, the condition is met, so the loop exits before the normal processing statement runs.

Mistakes That Prevent Exit

  • Common mistake

    Forgetting the break statement

    What it looks like

    The loop checks for the stop command but never executes break.

    Why it fails

    while True remains true, so the loop continues asking for input instead of leaving the loop.

    Do this instead

    Place break in the branch that handles the intended exit command.

  • Common mistake

    Using the wrong exit command

    What it looks like

    The program checks for done, but the user is told to enter quit.

    Why it fails

    The condition is not met when the user enters a different command, so break is skipped.

    Do this instead

    Make the user instruction and the condition use the same exit command.

  • Common mistake

    Misplacing break outside the condition

    What it looks like

    break is placed so that it runs on every iteration.

    Why it fails

    The loop exits before it can process ordinary input.

    Do this instead

    Put break inside the condition that identifies the stop command.

Best practice

When building an interactive loop, include the exit condition deliberately, test the exact command that should stop the loop, and tell the user how to stop it. These checks make the loop's control flow predictable.

Practice the Trace

EASY

Trace the generated input loop for the inputs red, blue, done. Write down what is printed during each iteration, identify the iteration where break runs, and state what is printed after the loop.

Hints
  • For each input, first compare it with done.
  • Only inputs that are not done reach the print statement inside the loop.
  • After break, execution continues with the statement after the loop.
Worked example

Practice solution

Trace the loop for red, blue, done.

  1. 1
    red

    red does not match done, so it is processed and printed.

  2. 2
    blue

    blue does not match done, so it is processed and printed.

  3. 3
    done

    done matches the exit condition, so break exits the loop before the input is printed.

  4. 4
    After exit

    The statement after the loop runs and prints Done!.

Result

The output contains red, blue, and then Done!. The word done is not printed by the loop.

Control Flow Summary

  1. 1
    while True creates a loop whose condition is always true, so it continues until a control statement exits it.
  2. 2
    An interactive input loop repeatedly prompts, checks the input, and either processes the input or stops.
  3. 3
    break immediately exits the loop and skips any remaining code in the current iteration.
  4. 4
    continue skips the remaining code in the current iteration and begins the next iteration instead of exiting the loop.
  5. 5
    A reliable loop needs a correctly placed break, the intended exit command, and clear instructions for the user.

At a glance

  • while True intentionally creates an indefinitely repeating loop.
  • break provides the explicit exit when a condition such as an input matching done is met.
  • Inputs that do not match the exit command are processed before the loop repeats.
  • break exits the loop, whereas continue skips the rest of one iteration and starts the next.
  • Tracing the condition check for each input reveals exactly when the loop repeats and when it ends.

Related concepts

More concepts starting with T