Using continue Statements
continue skips the rest of the current iteration. It does not end the entire loop.
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 Detour
A loop can encounter an item that should not be processed. Sometimes the correct response is not to stop the loop, but to skip that one item and keep going. Python's continue statement provides that selective detour: it ends the current iteration immediately and sends execution back to the top of the loop.
continue skips the rest of the current iteration. It does not end the entire loop.
The Jump Back to the Loop
When Python reaches continue, it immediately stops executing the current loop body. Every statement located after continue in that loop block is skipped for the current iteration. Execution then returns to the top of the loop, where the next iteration begins. If the condition leading to continue is false, Python does not jump away and continues executing the remaining statements in the current iteration.
The diagram shows the two possible paths. A true skip condition reaches continue and bypasses the remaining loop-body statements. A false condition allows those statements to run before the loop proceeds.
Tracing a Filtered Loop
for number in [1, 2, 3, 4]:
if number == 2:
continue
print(number)- 1For number equal to 1, the condition is false, so 1 is printed.
- 2For number equal to 2, continue executes, so the print statement is skipped.
- 3For number equal to 3, the condition is false, so 3 is printed.
- 4For number equal to 4, the condition is false, so 4 is printed.
- The loop begins an iteration with the next value.
- When number is 2, continue skips the print statement for that iteration.
- For the other values, the condition is false and print executes.
1
3
4What do you think happens?
What values are printed by the loop before you read the answer?
Reveal answer
1, 3, 4
The iteration for 2 reaches continue, so the print statement after it is skipped. The loop continues with 3 and then 4.
The important tracing habit is to inspect each iteration separately. Ask whether the skip condition is true. If it is true, cross out every statement after continue in that iteration. Then move to the next loop value. Do not cross out the same statements for later iterations unless their conditions also trigger continue.
Filtering Inputs Without Stopping
A common use of continue is to skip input that does not meet a minimum requirement. The source example uses a loop that accepts user input and checks its length. If the input is shorter than three characters, the condition len(user_input) < 3 is true. continue then skips the processing statement and returns to the top of the loop. If the input is at least three characters long, continue is not executed and the program prints that the input is of sufficient length.
| Input | Condition | Effect |
|---|---|---|
| a | len(user_input) < 3 is true | continue skips the final print statement |
| 12 | len(user_input) < 3 is true | continue skips the final print statement |
| abc | len(user_input) < 3 is false | The program prints that the input is of sufficient length |
How the input length determines the loop path
Continue Versus Break
| Statement | Effect on current iteration | Effect on the loop |
|---|---|---|
| continue | Stops the current iteration and skips the remaining loop-body statements | The loop proceeds to its next iteration |
| break | Stops execution at the point where it is encountered | The loop exits entirely |
The essential control-flow difference
Both statements alter normal loop execution, but they answer different questions. Choose continue when the current iteration should be abandoned and later iterations should still be considered. Choose break when the loop should end completely. Confusing these effects can cause a loop to process too much or to stop earlier than intended.
Mistakes in Execution Traces
Common mistake Treating continue as if it ended the entire loop
What it looks likeA loop reaches continue for one value, and the trace stops there.
Why it failscontinue skips only the current iteration. The loop returns to the top and begins the next iteration.
Do this insteadAfter recording the skipped statements, continue tracing the later iterations.
Common mistake Executing statements after continue in the same iteration
What it looks likeA print statement below continue is included in the output for the value that triggered continue.
Why it failsAll code between continue and the end of the loop block is skipped for that iteration.
Do this insteadMark those statements as skipped, then move directly to the next iteration.
Common mistake Assuming continue runs on every iteration
What it looks likeThe trace skips the processing statement even when the condition leading to continue is false.
Why it failscontinue is reached only when the relevant condition is true.
Do this insteadEvaluate the condition separately for every loop iteration.
if len(user_input) < 3:
continue
print("Input is of sufficient length")For a short input, the print statement is incorrectly treated as if it runs.
if len(user_input) < 3:
continue
# The print statement runs only when the condition is false.For a short input, continue skips the print statement and the next iteration begins.
Trace It Yourself
Trace the loop one iteration at a time. Which values reach the print statement, and which value causes continue to run?
for value in ["a", "cat", "12", "hello"]:
if len(value) < 3:
continue
print(value)Hints
- Evaluate
len(value)< 3 for each value. - When the condition is true, skip the print statement for that iteration.
- Keep tracing after a continue; it does not end the loop.
Checking Each Value
Determine the output for the loop that processes a, cat, 12, and hello while skipping values shorter than three characters.
- 1a
The length is below 3, so continue runs and the value is not printed.
- 2cat
The length is not below 3, so continue does not run and cat is printed.
- 312
The length is below 3, so continue runs and the value is not printed.
- 4hello
The length is not below 3, so continue does not run and hello is printed.
cathello
Key Takeaways
- 1continue immediately ends the current loop iteration.
- 2Statements after continue in the same loop block are skipped for that iteration.
- 3After continue, execution returns to the top of the loop and begins the next iteration.
- 4continue is useful for filtering or skipping selected inputs without ending the loop.
- 5break exits the entire loop, while continue affects only the current iteration.
At a glance
- continue skips the rest of the current iteration and starts the next one.
- A condition can use continue to reject selected inputs while allowing the loop to process later inputs.
- To trace a loop, evaluate the continue condition for each iteration and skip only the statements below continue in that iteration.
- continue keeps the loop running, whereas break exits the loop entirely.