Concepts / Nested Conditionals in Loops

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.

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

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.

Key point

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.

Loop topnext iterationConditionspecial input?continueskip remainderNext iterationloop topRemaining codenot executedread inputtruefalsejump
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
Worked example

Following three input lines

Trace the filter for the inputs #private, hello, and #later.

  1. 1
    Input #private

    The line starts with #, so the condition is true. continue runs, print(line) is skipped, and the loop returns to its top.

  2. 2
    Input hello

    The line does not start with #, so continue does not run. Execution reaches print(line), and hello is printed.

  3. 3
    Input #later

    The line starts with # again. continue skips print(line), and the loop returns to its top for another input.

Result

Only hello reaches print(line). The two comment lines are filtered out while the loop continues accepting input.

Iteration 1#privatecontinueprint skippedIteration 2helloprint(line)hello printedIteration 3#latercondition truereturn to loop topcondition falsenext iteration
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
Worked example

Choosing between continue and break

Trace the inputs #note, hello, and done in the combined loop.

  1. 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.

  2. 2
    hello

    The input is not done and does not start with #. Neither control statement runs, so print(line) processes the input.

  3. 3
    done

    The termination condition is true, so break exits the loop entirely. The later conditional and print statement are not reached for this input.

Result

The comment is skipped, hello is processed, and done ends the loop.

Input lineeach iterationdonetermination checkbreakloop endscontinueskip lineComment linestarts with #Process linenormal inputreadtruefalsetruefalse
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

The essential difference between continue and break

Placement and Tracing Mistakes

  • Common mistake

    Treating continue as if it ended the loop.

    What it looks like

    A comment line triggers continue, but the learner expects no later input to be read.

    Why it fails

    continue skips only the remainder of the current iteration. The loop returns to its top and continues running.

    Do this instead

    Use break when the requirement is to exit the loop entirely.

  • Common mistake

    Forgetting that statements after continue are skipped.

    What it looks like

    The learner expects print(line) to run after continue in the same iteration.

    Why it fails

    continue immediately returns control to the top of the loop, so later statements in that iteration are not executed.

    Do this instead

    Trace 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 like

    The loop reaches continue without first checking whether the line starts with #.

    Why it fails

    The statement would skip iterations without being limited to the intended special case.

    Do this instead

    Place 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 like

    A line beginning with # is treated as though it had the same effect as done.

    Why it fails

    The filter uses continue for comment lines and break for the termination input. They have different effects.

    Do this instead

    Ask whether the loop should accept another input. If yes, use continue; if no, use break.

Best practice

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

Related concepts

More concepts starting with N