The for Loop: Iterating Over Sequences
A while loop repeats code as long as a condition is true, checking the condition before each iteration.
Why Repetition Needs a Loop
Suppose you need to print the numbers 5, 4, 3, 2, and 1, followed by “Blastoff!”. You could write five separate print statements. But counting down from 1000 would require an impractical number of statements. A loop lets you write a small block of code once and have Python execute it repeatedly, with slight variations each time.
The supplied material focuses on the while loop. A while loop repeatedly executes a block of code as long as a condition remains true. Python checks the condition before every possible execution of the loop body. If the condition is true, the body runs. If the condition is false, the loop stops and the program continues with the next statement.
The Countdown Execution Path
Counting Down from Five
Determine what happens when n starts at 5, the loop continues while n > 0, and the body prints n before decreasing it.
Initial check: Python evaluates the condition with n equal to 5. Because 5 is greater than 0, the condition is true and the loop body runs.
First iteration: The body prints 5 and then decreases n to 4.
Middle iterations: The same process occurs for n equal to 4, 3, 2, and 1. Each value is printed, and n decreases by one.
Final check: After the iteration where n is 1, n becomes 0. Python checks n > 0 again. The condition is false, so the loop exits.
Following statement: After the loop exits, the program prints “Blastoff!”.
The loop prints 5, 4, 3, 2, 1 and then prints “Blastoff!”.
Condition Checks and Iteration Variables
The execution flow has a fixed order. Python first evaluates the condition, which produces either True or False. If the result is False, the loop exits immediately. If the result is True, Python executes the loop body and then returns to the beginning to evaluate the condition again.
An iteration is one complete execution of the loop body. If the body executes five times, the loop has five iterations.
The iteration variable is the variable that changes inside the loop body and eventually causes the condition to become false. In the countdown, n is the iteration variable. The body decreases n so that the value moves toward the condition becoming false.
When the Loop Cannot Finish
An infinite loop occurs when the condition never becomes false. One cause is that the iteration variable never changes. Another cause is that it changes in a direction that keeps the condition true. For example, with a condition such as n > 0, failing to decrease n means that n remains greater than 0, so the loop continues indefinitely.
Leaving the iteration variable unchanged
The condition remains true because n never moves toward zero.
Fix:
Modify the iteration variable inside the body so that it moves toward making the condition false.Changing the variable in the wrong direction
The change keeps the condition true instead of moving toward termination.
Fix:
Check whether the update moves the variable toward the condition becoming false.Assuming the body runs before the first condition check
A while loop checks its condition before every execution, including the first possible iteration.
Fix:
Trace the initial condition before counting any iteration.Miscounting iterations
An iteration means one complete pass through the body, not one condition check that returns False.
Fix:
Count only the times the loop body actually executes.
Manual Tracing for Debugging
When a loop produces unexpected output, trace it one step at a time. Before each possible iteration, write down the current value of the iteration variable and the result of the condition. Then record what the body does and what value the variable has afterward. This exposes the exact point where execution diverges from your expectation.
| Check | Value of n | Condition n > 0 | Body executes? |
|---|---|---|---|
| 1 | 5 | True | Yes |
| 2 | 4 | True | Yes |
| 3 | 3 | True | Yes |
| 4 | 2 | True | Yes |
| 5 | 1 | True | Yes |
| 6 | 0 | False | No |
A manual trace separates condition checks from actual iterations. The sixth check does not create a sixth iteration because the body does not execute.
What do you think happens?
A countdown starts with n equal to 5 and continues while n > 0. How many iterations occur before the condition becomes false?
Reveal answer
Answer: 5
The body executes when n is 5, 4, 3, 2, and 1. After the fifth iteration, n becomes 0. The next condition check is false, so it exits without a sixth iteration.
Practice the Trace
Create a trace for a countdown whose iteration variable starts at 3, whose condition is that the variable is greater than 0, and whose body decreases the variable by 1. Record the variable's value before each condition check, whether the body runs, and the total number of iterations.
Hints
- List the values tested before the body runs.
- Remember that the condition is checked once more after the final iteration.
- Count body executions, not all condition checks.
A loop is expected to run five times, but its output suggests that it runs six times. Describe the trace information you would write down to find the extra iteration.
Hints
- Record the iteration variable before every body execution.
- Check the value immediately after the fifth body execution.
- Inspect the condition that permits the next pass.
Key Takeaways
- A while loop checks its condition before each possible iteration.
- A true condition runs the body; a false condition exits the loop and continues with the next statement.
- An iteration is one complete execution of the loop body.
- The iteration variable must change toward making the condition false.
- Manual tracing records the variable and condition at each check and helps reveal logic errors.
Key Takeaways
- A while loop repeats a body while its condition remains true.
- Python checks the condition before every iteration, including the first possible one.
- The iteration variable must move toward making the condition false, or the loop may become infinite.
- Tracing values and condition results helps distinguish iterations from checks and exposes unexpected behavior.