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.
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
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- 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').
- 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.
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.
The Jump Made by continue
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
while True:
line = input()
if line[0] == '#':
continue
print(line)- 1The loop reads a line.
- 2The conditional checks the first character of that line.
- 3For a line beginning with '#', continue executes.
- 4Because continue executes,
print(line)is not executed for that line. - 5Control 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.
What do you think happens?
Suppose the input line begins with '#'. Which action occurs for that iteration?
Reveal 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.
Combining continue and break
- 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).
| Input condition | Statement | Effect |
|---|---|---|
| The line is 'done' | break | Exit the loop entirely |
| The first character is '#' | continue | Skip the current iteration and accept more input |
| Neither condition is true | print(line) | Process the line through the remaining statement |
The distinct control-flow roles of break and continue
Infinite Iteration and Exit
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 likeA comment line triggers continue, but the program is expected to stop accepting input.
Why it failscontinue skips only the current iteration. The loop continues running.
Do this insteadUse break when a condition should exit the loop entirely.
Common mistake Assuming statements after continue still run in the same iteration.
What it looks likeA line begins with '#', and the code is expected to reach print(line).
Why it failscontinue skips the remainder of the current iteration, so the later print statement is not executed for that line.
Do this insteadTrace the path from continue directly back to the top of the loop.
Common mistake Placing continue outside a condition.
What it looks likeThe loop executes continue before testing whether the input should be skipped.
Why it failsThe rest of every iteration is skipped instead of only the iterations matching a specific condition.
Do this insteadPlace continue inside an if statement or another conditional block.
Common mistake Using continue for the termination input.
What it looks likeThe input done triggers continue when it should end the loop.
Why it failscontinue keeps the loop running and accepts another input.
Do this insteadUse break for the input that signals the end of the loop.
Practice the Trace
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.
Tracing Three Inputs
Determine the control-flow action for a comment line, an ordinary line, and done.
- 1Comment line
The line is not done, but its first character is '#'. The program executes continue, skips
print(line), and returns to the top. - 2Ordinary line
The line is not done and does not begin with '#'. The program does not execute either control statement, so
print(line)runs. - 3done
The termination condition is true, so break executes. The loop exits before the comment test and print statement are reached.
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.