Loop Control Flow
Statements after continue in the same loop block do not run for the iteration that reached continue. They can run during later iterations if those iterations do not reach continue.
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…
A Selective Skip
A loop does not always need to process every iteration in the same way. Sometimes one value should be skipped while the loop continues handling later values. Python provides continue for this situation. When Python reaches continue, it stops executing the current iteration and returns to the top of the loop to begin the next iteration.
The Continue Jump
A continue statement divides the current loop iteration into two possible paths. If execution reaches continue, Python immediately stops the current iteration. Every statement after continue and before the end of the loop block is skipped for that iteration. Execution then returns to the top of the loop, where the next iteration begins. If the condition that leads to continue is not met, execution proceeds through the remaining statements in the loop block normally.
Statements after continue in the same loop block do not run for the iteration that reached continue. They can run during later iterations if those iterations do not reach continue.
Tracing an Iteration
Tracing the input sequence
For the input sequence hi, hello, x, and stop, determine what happens when the loop skips inputs shorter than three characters.
- 1hi
The input has fewer than three characters, so continue is reached. Processing after continue is skipped, and the loop returns to the top for the next input.
- 2hello
The input meets the minimum length requirement, so continue is not reached. The processing statement runs before the loop begins its next iteration.
- 3x
The input has fewer than three characters, so continue is reached. Processing after continue is skipped, and the loop moves to the next iteration.
- 4stop
This input is part of the stated sequence. The continue rule concerns the minimum length, so this input meets that requirement and the processing statement runs.
The short inputs hi and x are skipped by continue. The processing statement runs for hello and stop.
What do you think happens?
Which inputs reach the processing statement: hi, hello, x, or stop?
Reveal answer
hello and stop
hi and x are shorter than three characters, so continue skips the processing statement for those iterations. hello and stop meet the minimum length requirement, so their processing statements run.
Filtering Without Exiting
A common use of continue is filtering. The loop can inspect each input, skip inputs that do not meet a minimum requirement, and continue processing later inputs. In the source example, strings shorter than three characters are not processed. The loop does not end after encountering a short string; it asks for or handles the next iteration instead.
Place continue at the point where the loop can clearly decide that the current item should be skipped. Then keep the processing statements after it. This makes the skip path and the process path easier to trace.
Continue Compared with Break
Continue and break both alter loop execution, but they do not have the same scope. Continue stops only the current iteration and returns to the top of the loop. Break exits the loop entirely. Use continue when later iterations should still be considered. Use break when the loop itself should end.
| Statement | Immediate effect | What happens afterward |
|---|---|---|
| continue | Stops the current iteration | Execution returns to the top of the loop for the next iteration |
| break | Exits the loop entirely | The loop does not continue with later iterations |
The essential difference between continue and break
Mistakes in Control Tracing
Common mistake Treating continue as if it ends the loop
What it looks likeA short input reaches continue, and the learner assumes no later inputs will be handled.
Why it failsContinue skips only the rest of the current iteration. Execution returns to the top of the loop for the next iteration.
Do this insteadTrace the next iteration after every continue before deciding whether the loop has ended.
Common mistake Counting statements after continue as executed for that iteration
What it looks likeThe learner includes the final processing statement in the output for a short input.
Why it failsStatements between continue and the end of the loop block are skipped during that iteration.
Do this insteadMark the current iteration as skipped from the continue point onward.
Common mistake Using the effect of break when explaining continue
What it looks likeThe learner says that continue exits the entire loop.
Why it failsBreak exits the loop entirely, while continue returns to the top of the loop.
Do this insteadAsk whether later iterations should still run. If they should, the described behavior is continue, not break.
Practice the Trace
Trace the source input sequence hi, hello, x, and stop. For each input, write whether continue is reached, whether the processing statement runs, and which input is handled next.
Hints
- Compare each input with the minimum length of three characters.
- If continue is reached, skip the processing statement for that iteration.
- After a continue, trace the next input rather than ending the loop.
Key Takeaways
- 1Continue stops the current loop iteration immediately.
- 2Statements after continue in the same loop block are skipped for that iteration.
- 3Execution returns to the top of the loop and begins the next iteration.
- 4Continue is useful for skipping selected inputs while still processing later inputs.
- 5Break exits the entire loop, so it has a different effect from continue.
At a glance
- Continue skips the rest of only the current iteration.
- After continue, execution returns to the top of the loop for the next iteration.
- Statements after continue do not execute during the iteration that reaches it.
- Continue can filter out selected inputs without ending the loop.
- Break exits the entire loop, unlike continue.