Explain when a repetition ends
Quick review (2 minutes)
A cashier counting a drawer of coins repeats one small action: take a coin and add it to the count.
A loop repeats its steps until its stopping condition is met. If the stopping condition can never be met, the loop never ends. To explain why a loop ends, name its stopping condition and the step in each pass that moves toward it.
Find the steps that repeat, and mark the step that sends the reader back.
Find the stopping condition, which is the test that lets the reader leave the loop.
Find the step inside the loop that changes what the stopping condition tests. In the largest-card procedure, taking a card changes how many cards are left.
Check the direction of that change. In the largest-card procedure every pass leaves one card fewer in the row, so the row runs out.
Trace a small input and count the passes. For a row of three cards, step 3 is carried out twice.
Write one sentence that names the stopping condition and the step that moves toward it.
A loop can look finished on paper because the tracer stops when the answer seems clear. Trace until the stopping condition is met, and note the step at which the reader leaves the loop.
The serious slip is a stopping condition that the passes cannot reach. Take a procedure invented for this paragraph. It writes 1, then repeats two steps: add 2 to the number, and stop if the number is 10. The number goes 3, 5, 7, 9, 11 and on, and never equals 10. The ending condition never becomes true, so the loop does not end. Compare the wording of the stopping condition with what each pass changes.
1 In plain words
A cashier counting a drawer of coins repeats one small action: take a coin and add it to the count. Nobody tells the cashier how many times to do it. The drawer does. When it is empty, the counting ends.
In an algorithm, iteration is a part that repeats. It repeats a set number of times or until a condition is met. The repeating part is often called a loop, and one trip through it is a pass.
In the largest-card procedure from lesson 2, steps 2 to 4 form a loop. Step 2 holds the stopping condition: when no card is left, go to step 5. Step 1 uses the first card, and each later card is compared once in step 3. The Field Guide counts its own example the same way: given 10 items, its loop runs 9 times.
Remember one sentence: to explain why a loop ends, name its stopping condition and say why it is reached. For iteration, find essential knowledge AAP-2.J.1 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of row sizes and the number of comparisons the largest-card procedure makes in step 3. A row of 1 card needs 0 comparisons. A row of 2 cards needs 1. A row of 3 cards needs 2. A row of 10 cards needs 9. The last row is marked.
3 Why
A loop ends for a reason you can point to. In the largest-card procedure the reason has two parts. The row holds a fixed number of cards, and every pass takes one card that is never put back. After the last card has been taken, step 2 finds no card and sends the reader to step 5.
A loop whose stopping condition can never become true does not end. That is an infinite loop. Suppose step 2 told the reader to look at the next card and leave it where it is, without moving along the row. On a row of two or more cards, the reader would compare the same card with the noted number on every pass, and the row would never run out.
4 The rule
A loop repeats its steps until its stopping condition is met.
If the stopping condition can never be met, the loop never ends. To explain why a loop ends, name its stopping condition and the step in each pass that moves toward it.
5 How
- Find the steps that repeat, and mark the step that sends the reader back.
- Find the stopping condition, which is the test that lets the reader leave the loop.
- Find the step inside the loop that changes what the stopping condition tests. In the largest-card procedure, taking a card changes how many cards are left.
- Check the direction of that change. In the largest-card procedure every pass leaves one card fewer in the row, so the row runs out.
- Trace a small input and count the passes. For a row of three cards, step 3 is carried out twice.
- Write one sentence that names the stopping condition and the step that moves toward it.
6 Trace
| Step 2 reached | Card taken | Cards left | Leave the loop? | Explanation |
|---|---|---|---|---|
| before the loop | 4 | 2 | — | Step 1 uses the first card. Two cards are left. |
| first time | 9 | 1 | no | Step 2 takes a card, so one is left. Step 3 compares 9 with the noted number. |
| second time | 2 | 0 | no | Step 2 takes the last card. Step 3 compares 2 with the noted number. |
| third time | none | 0 | yes | Step 2 finds no card left. The stopping condition is met, so go to step 5. |
7 The trap
A loop can look finished on paper because the tracer stops when the answer seems clear. Trace until the stopping condition is met, and note the step at which the reader leaves the loop.
The serious slip is a stopping condition that the passes cannot reach. Take a procedure invented for this paragraph. It writes 1, then repeats two steps: add 2 to the number, and stop if the number is 10. The number goes 3, 5, 7, 9, 11 and on, and never equals 10. The ending condition never becomes true, so the loop does not end. Compare the wording of the stopping condition with what each pass changes.
Quick checks
Quick check 1 of 2
The largest-card procedure is carried out on a row of 5 cards. How many times is the comparison in step 3 carried out, and which step ends the loop?
Show the steps and the answer
Step 3 is carried out 4 times. Step 2 ends the loop when no card is left.
Why: Step 1 uses the first card without a comparison. Each of the other 4 cards is compared once in step 3. After the last card, step 2 finds none and goes to step 5.
Quick check 2 of 2
A procedure invented for this question has four steps. Step 1: write 10. Step 2: if the number is 0, stop. Step 3: add 1 to the number. Step 4: go back to step 2. Does the loop end? Rewrite step 3 so that it does.
Show the steps and the answer
It does not end. Rewrite step 3 as: subtract 1 from the number.
Why: The stopping condition tests for 0. Adding 1 moves the number away from 0 on every pass: 11, 12, 13 and so on. Subtracting 1 moves it toward 0, which it reaches after 10 passes.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 1 Dec 2026.
- Computer Science Field Guide: Algorithms, 2.1 What's the big picture?. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 1 Dec 2026.