Concepts / Nested Conditionals in Loops

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.

  • Programming

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.

read inputtruefalsejumpLoop topnext iterationConditionspecial input?continueskip remainderNext iterationloop topRemaining codenot executed
When the conditional is true, which statements are skipped and where does control flow go next?

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.

python

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.

condition truereturn to loop topcondition falsenext iterationIteration 1#privatecontinueprint skippedIteration 2helloprint(line)hello printedIteration 3#later
How does the loop move from the current iteration back to the next iteration after continue runs?

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.

python

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.

readtruefalsetruefalseInput lineeach iterationdonetermination checkbreakloop endscontinueskip lineComment linestarts with #Process linenormal input
What happens when one input triggers continue, another triggers break, and a normal input reaches the loop body?
StatementEffect on current iterationEffect on loop
continueSkips the remaining codeKeeps the loop running
breakStops the current pathExits 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?

  • The loop ends on #skip
  • Only keep is processed, then done ends the loop
  • All three inputs are processed
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.

MEDIUM

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

  1. continue skips the remainder of the current loop iteration and returns control to the top of the loop.
  2. A conditional should control continue so only selected inputs are skipped.
  3. A filter can use continue to ignore comment lines while continuing to accept input.
  4. break exits the loop entirely, while continue keeps the loop running.
  5. 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.