Nested Conditionals in Loops
Use continue when the loop should keep running but the current input should not reach the remaining statements in the loop body.
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…
Selective Loop Control
A loop may receive different kinds of input. Some lines should be processed, some should be ignored, and one particular input may signal that the loop is finished. The continue statement handles the ignore case: it skips the rest of the current iteration and returns control to the top of the loop for the next iteration.
Use continue when the loop should keep running but the current input should not reach the remaining statements in the loop body.
The Continue Path
The important control-flow question is not merely whether a condition is true. It is what happens immediately afterward. If the conditional containing continue is true, every statement later in the current loop body is skipped. Execution returns to the top of the loop, where the next iteration begins. In a while True loop, that means the loop begins its next pass and accepts another input.
Tracing a Filter
Consider a filter that ignores comment lines. A line beginning with the character # is treated as unwanted input. When that condition is true, continue runs before the line-printing statement, so the comment line is not printed. The loop then returns to its top and accepts another line.
Following three input lines
Trace the filter for the inputs #private, hello, and #later.
- 1Input #private
The line starts with #, so the condition is true. continue runs,
print(line)is skipped, and the loop returns to its top. - 2Input hello
The line does not start with #, so continue does not run. Execution reaches
print(line), and hello is printed. - 3Input #later
The line starts with # again. continue skips
print(line), and the loop returns to its top for another input.
Only hello reaches print(line). The two comment lines are filtered out while the loop continues accepting input.
Continue and Break Together
A loop can use both statements because they answer different control-flow needs. continue skips the current iteration and keeps the loop running. break exits the loop entirely. In a filter program, continue can ignore lines beginning with #, while break can stop the loop when the user types done.
Choosing between continue and break
Trace the inputs #note, hello, and done in the combined loop.
- 1#note
The input is not done, so break is not triggered. It starts with #, so continue skips
print(line)and begins the next iteration. - 2hello
The input is not done and does not start with #. Neither control statement runs, so
print(line)processes the input. - 3done
The termination condition is true, so break exits the loop entirely. The later conditional and print statement are not reached for this input.
The comment is skipped, hello is processed, and done ends the loop.
| Statement | Effect on current iteration | Effect on loop |
|---|---|---|
| continue | Skips the remaining code | Keeps the loop running |
| break | Stops the current path | Exits the loop entirely |
The essential difference between continue and break
Placement and Tracing Mistakes
Common mistake Treating continue as if it ended the loop.
What it looks likeA comment line triggers continue, but the learner expects no later input to be read.
Why it failscontinue skips only the remainder of the current iteration. The loop returns to its top and continues running.
Do this insteadUse break when the requirement is to exit the loop entirely.
Common mistake Forgetting that statements after continue are skipped.
What it looks likeThe learner expects print(line) to run after continue in the same iteration.
Why it failscontinue immediately returns control to the top of the loop, so later statements in that iteration are not executed.
Do this insteadTrace the path in order: condition, continue, loop top. Do not include the remaining statements in that iteration.
Common mistake Placing continue outside the condition that identifies unwanted input.
What it looks likeThe loop reaches continue without first checking whether the line starts with #.
Why it failsThe statement would skip iterations without being limited to the intended special case.
Do this insteadPlace continue inside a conditional block so it runs only when the specific condition is met.
Common mistake Confusing a skipped input with a terminated loop.
What it looks likeA line beginning with # is treated as though it had the same effect as done.
Why it failsThe filter uses continue for comment lines and break for the termination input. They have different effects.
Do this insteadAsk whether the loop should accept another input. If yes, use continue; if no, use break.
Place continue inside a conditional block, usually an if statement, so the skip occurs only for the input that matches the intended condition. This keeps the loop's normal processing path visible and avoids unnecessary nesting of if-else blocks.
Control-Flow Practice
What do you think happens?
For the combined filter loop, what happens when the input sequence is #skip, keep, done?
Reveal answer
Only keep is processed, then done ends the loop.
#skip triggers continue, so it is skipped while the loop continues. keep reaches the processing statement. done triggers break, so the loop exits.
Write the execution path for these inputs in order: #draft, visible, #internal, done. For each input, identify whether continue runs, break runs, or neither runs, and state whether the processing statement is reached.
Hints
- Check the termination condition before the comment condition.
- A line beginning with # triggers continue.
- The input done triggers break and ends the loop.
Key Takeaways
- 1continue skips the remainder of the current loop iteration and returns control to the top of the loop.
- 2A conditional should control continue so only selected inputs are skipped.
- 3A filter can use continue to ignore comment lines while continuing to accept input.
- 4break exits the loop entirely, while continue keeps the loop running.
- 5When tracing nested conditionals, identify the first triggered control statement and then remove all later statements from that iteration's path.
At a glance
- continue skips the rest of one iteration and begins the next iteration.
- A conditional lets continue target unwanted inputs such as lines beginning with #.
- break and continue are complementary: break ends the loop, while continue preserves it.
- Tracing becomes straightforward when you mark the condition that triggered and omit every later statement in that iteration.