Nested Conditionals in Loops
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.
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.
Input #private: The line starts with #, so the condition is true. continue runs, print(line) is skipped, and the loop returns to its top.
Input hello: The line does not start with #, so continue does not run. Execution reaches print(line), and hello is printed.
Input #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.
#note: The input is not done, so break is not triggered. It starts with #, so continue skips print(line) and begins the next iteration.
hello: The input is not done and does not start with #. Neither control statement runs, so print(line) processes the input.
done: 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 |
Placement and Tracing Mistakes
Treating continue as if it ended the loop.
continue skips only the remainder of the current iteration. The loop returns to its top and continues running.
Fix:
Use break when the requirement is to exit the loop entirely.Forgetting that statements after continue are skipped.
continue immediately returns control to the top of the loop, so later statements in that iteration are not executed.
Fix:
Trace the path in order: condition, continue, loop top. Do not include the remaining statements in that iteration.Placing continue outside the condition that identifies unwanted input.
The statement would skip iterations without being limited to the intended special case.
Fix:
Place continue inside a conditional block so it runs only when the specific condition is met.Confusing a skipped input with a terminated loop.
The filter uses continue for comment lines and break for the termination input. They have different effects.
Fix:
Ask 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
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
- continue skips the remainder of the current loop iteration and returns control to the top of the loop.
- A conditional should control continue so only selected inputs are skipped.
- A filter can use continue to ignore comment lines while continuing to accept input.
- break exits the loop entirely, while continue keeps the loop running.
- When tracing nested conditionals, identify the first triggered control statement and then remove all later statements from that iteration's path.
Key Takeaways
- 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.