Loops and Iteration
The defining idea is repetition: control reaches the top of the loop again, and the loop body can execute another iteration.
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.
- 1First iteration
The loop body executes. Because continue has not been reached in this example, the remaining statements in the body execute.
- 2Second iteration
Python reaches continue. The remaining statements in the current loop body are skipped, and control proceeds to the next iteration.
- 3Third 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
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
Common mistake Treating continue as a command that ends the entire loop.
What it looks likeA learner sees continue during one iteration and expects no later iterations.
Why it failscontinue skips the rest of the current loop block and proceeds to the next iteration.
Do this insteadTrace the current iteration and then move to the next one.
Common mistake Counting only complete loop bodies as iterations.
What it looks likeA learner says a loop had two iterations because the third iteration reached continue.
Why it failsAn iteration is each time the body of the loop executes, even when continue skips part of that body.
Do this insteadCount every execution of the loop body, including an execution that reaches continue.
Common mistake Assuming repetition automatically ends.
What it looks likeA loop repeats without changing a variable that controls when it finishes.
Why it failsWithout an iteration variable that changes toward the stopping condition, the loop can repeat forever.
Do this insteadCheck 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
- 1A loop repeats a body of statements, and each execution of that body is an iteration.
- 2An iteration variable changes as the loop runs and helps the loop eventually terminate.
- 3continue skips the remaining statements in the current loop block.
- 4After continue, control proceeds to the next iteration.
- 5If no iteration variable changes to make the loop condition false, the loop can become infinite.
At a glance
- 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.