Concepts / while True Loops and Infinite Iteration

while True Loops and Infinite Iteration

The continue statement skips the rest of the current loop iteration and jumps back to the top of the loop to begin the next iteration.

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

Why Skip an Iteration

A loop that accepts user input may receive different kinds of lines. Some lines may be comments that should be ignored, while another input may signal that the loop should end. The continue statement handles the first situation: it skips the rest of the current iteration and returns to the top of the loop. The break statement handles the second: it exits the loop entirely.

Loop topnext inputSkip conditiontrue or falsecontinuecurrent iteration endsLater statementsskippedNext iterationloop topexecutetruefalsejumpfinish
What happens to the current iteration after continue executes?

The Jump Made by continue

Definition

The continue statement skips the remainder of the current loop iteration and immediately returns to the top of the loop to begin the next iteration.

The important detail is what continue skips. It does not merely skip one statement. It skips every remaining statement in the current loop body. Control then returns to the top of the loop, where the next iteration begins. In a while True loop, this means the loop continues accepting input unless another path, such as break, exits it.

Tracing a Comment Line

Code walkthrough
while True:
    line = input()

    if line[0] == '#':
        continue

    print(line)
  1. 1
    The loop reads a line.
  2. 2
    The conditional checks the first character of that line.
  3. 3
    For a line beginning with '#', continue executes.
  4. 4
    Because continue executes, print(line) is not executed for that line.
  5. 5
    Control returns to the top of the loop, where the program accepts another line.
  • Read an input line.
  • Test whether its first character is '#'.
  • If the condition is true, continue skips the remaining statements in this iteration.
  • The print statement is reached only when the condition is false.
Input linelineline[0] == '#'conditioncontinueskip remainderprint(line)later statementLoop topnext inputchecktruefalsereturnnext iteration
When the condition is true, which statement is skipped and where does control go next?

What do you think happens?

Suppose the input line begins with '#'. Which action occurs for that iteration?

  • The line is printed, then the loop continues.

  • The line is discarded from processing, and the loop returns to the top.

  • The loop exits entirely.

  • The conditional is ignored.

Reveal answer
Answer:

The line is discarded from processing, and the loop returns to the top.

When line[0] == '#' is true, continue executes. It prevents print(line) from running and starts the next iteration instead. It does not exit the loop.

Filtering Input Lines

A filter loop examines each input line and decides whether the line should receive the remaining processing. In this pattern, a line whose first character is '#' is treated as a comment. continue discards that line from the rest of the loop body, while other lines can reach print(line). The loop continues accepting more input after either path unless a separate termination condition uses break.

Input linesone at a timeComment linefirst character '#'continueskip processingOther linenot a commentprint(line)remaining processingNext inputloop continuesexamineyesnoprocessnext iterationnext iteration
How does each input line move through the loop, and which lines are discarded before later processing?

Combining continue and break

python
Notes on the code
  • The done condition uses break to exit the loop.
  • The comment condition uses continue to skip the current iteration.
  • A line that is neither done nor a comment reaches print(line).
Read lineinputline == 'done'termination testbreakloop exitsline[0] == '#'filter testcontinueskip remainderprint(line)ordinary lineLoop topnext iterationtesttruefalsetruefalsenext iterationnext iteration
How does control flow differ when an input triggers break, continue, or neither?
Input conditionStatementEffect
The line is 'done'breakExit the loop entirely
The first character is '#'continueSkip the current iteration and accept more input
Neither condition is trueprint(line)Process the line through the remaining statement

The distinct control-flow roles of break and continue

Infinite Iteration and Exit

Loop topwhile TrueInput linenew iterationcontinuekeep runningbreaktermination conditionNext iterationloop topLoop exitbreak pathstartskip casecheck exitreturntruefalseloop
How does a while True loop keep starting iterations, and what event allows it to stop?

The word True in while True means the loop keeps beginning new iterations unless control leaves the loop. continue does not provide that exit; it sends execution back to the top. In the input filter pattern, break provides the exit when the user types done, while continue keeps the loop running after a comment line.

Common Control-Flow Mistakes

  • Common mistake

    Treating continue as if it exits the loop.

    What it looks like

    A comment line triggers continue, but the program is expected to stop accepting input.

    Why it fails

    continue skips only the current iteration. The loop continues running.

    Do this instead

    Use break when a condition should exit the loop entirely.

  • Common mistake

    Assuming statements after continue still run in the same iteration.

    What it looks like

    A line begins with '#', and the code is expected to reach print(line).

    Why it fails

    continue skips the remainder of the current iteration, so the later print statement is not executed for that line.

    Do this instead

    Trace the path from continue directly back to the top of the loop.

  • Common mistake

    Placing continue outside a condition.

    What it looks like

    The loop executes continue before testing whether the input should be skipped.

    Why it fails

    The rest of every iteration is skipped instead of only the iterations matching a specific condition.

    Do this instead

    Place continue inside an if statement or another conditional block.

  • Common mistake

    Using continue for the termination input.

    What it looks like

    The input done triggers continue when it should end the loop.

    Why it fails

    continue keeps the loop running and accepts another input.

    Do this instead

    Use break for the input that signals the end of the loop.

Practice the Trace

MEDIUM

Trace the generated filter program using these inputs in order: a line beginning with '#', an ordinary line, and done. For each input, identify whether the program executes continue, break, or print(line). Then identify which input causes the loop to exit.

Hints
  • Check the termination condition before the comment condition.
  • continue skips the remaining statements for its current iteration.
  • break exits the loop entirely.
Worked example

Tracing Three Inputs

Determine the control-flow action for a comment line, an ordinary line, and done.

  1. 1
    Comment line

    The line is not done, but its first character is '#'. The program executes continue, skips print(line), and returns to the top.

  2. 2
    Ordinary line

    The line is not done and does not begin with '#'. The program does not execute either control statement, so print(line) runs.

  3. 3
    done

    The termination condition is true, so break executes. The loop exits before the comment test and print statement are reached.

Result

The comment line is skipped, the ordinary line is printed, and done ends the loop.

Key Takeaways

  • continue skips the remainder of the current loop iteration and returns to the top.
  • A conditional continue can filter unwanted input, such as lines beginning with '#'.
  • break exits the loop entirely, while continue keeps the loop running.
  • In a while True loop, a termination condition using break provides the path out of repeated iterations.
  • Trace the conditions in order to determine whether an input triggers break, continue, or the remaining statements.

Related concepts

More concepts starting with W