Loops and Iteration
The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.
A Repeated Path
A loop repeatedly executes a group of statements. Each time the body of the loop executes is called an iteration. If the body executes five times, the loop has five iterations.
The defining idea is repetition: control reaches the top of the loop again, and the loop body can execute another iteration.
Tracking Progress
A loop needs progress toward completion. The body of the loop should change one or more variables so that eventually the loop condition becomes false and the loop terminates. The variable that changes during execution and controls when the loop finishes is called the iteration variable.
Skipping the Current Body
The continue statement tells Python to skip the rest of the statements in the current loop block and continue to the next iteration of the loop.
The important boundary is the current loop body. Statements before continue have already been reached in that iteration. Statements after continue in the same loop body are skipped. Control then proceeds with the next iteration rather than finishing the skipped portion of the current one.
Tracing a Skip
A Three-Iteration Trace
Imagine a loop with three iterations. In the second iteration, Python reaches continue before the rest of the loop body.
First iteration: The loop body executes. Because continue has not been reached in this example, the remaining statements in the body execute.
Second iteration: Python reaches continue. The remaining statements in the current loop body are skipped, and control proceeds to the next iteration.
Third iteration: The loop body executes again. The remaining statements execute unless continue is reached in this iteration as well.
The loop has three iterations. The second iteration still counts as an iteration even though continue skips part of its body.
This trace separates two ideas that are easy to confuse. An iteration is one execution of the loop body. The statements after continue are only one portion of that body, so skipping them does not erase the iteration itself.
What do you think happens?
A loop reaches continue during its second iteration. Does the loop immediately finish all looping, or does it proceed to another iteration?
Reveal answer
Answer: It proceeds to the next iteration.
continue skips the rest of the statements in the current loop block and tells Python to continue with the next iteration.
Mistakes to Avoid
Treating continue as a command that ends the entire loop.
continue skips the rest of the current loop block and proceeds to the next iteration.
Fix:
Trace the current iteration and then move to the next one.Counting only complete loop bodies as iterations.
An iteration is each time the body of the loop executes, even when continue skips part of that body.
Fix:
Count every execution of the loop body, including an execution that reaches continue.Assuming repetition automatically ends.
Without an iteration variable that changes toward the stopping condition, the loop can repeat forever.
Fix:
Check which variable changes during each execution and how that change leads to termination.
Practice Trace
Consider a loop with four iterations. The loop reaches continue during the first and fourth iterations, but not during the second and third. Identify which iterations execute the remaining loop statements and state how many iterations the loop has.
Hints
- continue affects only the current loop iteration.
- Count every execution of the loop body, including iterations that reach continue.
- The remaining statements execute only in iterations where continue is not reached.
The expected trace is: the remaining statements are skipped in the first and fourth iterations, they execute in the second and third iterations, and the loop has four iterations in total.
Essential Takeaways
- A loop repeats a body of statements, and each execution of that body is an iteration.
- An iteration variable changes as the loop runs and helps the loop eventually terminate.
- continue skips the remaining statements in the current loop block.
- After continue, control proceeds to the next iteration.
- If no iteration variable changes to make the loop condition false, the loop can become infinite.
Key Takeaways
- A loop repeats its body, and each execution of the body is an iteration.
- An iteration variable provides progress toward the loop's end.
- continue skips the rest of the current loop body and moves control to the next iteration.
- A loop without progress toward its stopping condition can repeat forever.