Article
Repeating a set number of times
To draw a square you do the same thing four times: go forward, turn, go forward, turn... Writing the same lines again and again is slow, and it is hopeless if you need 1,000 copies. A computer can repeat a block of code for you. Repeating code is called iteration, and the code that does it is a loop.
Repeat a block a set number of times
The AP exam repeats code with REPEAT n TIMES. Everything inside the braces is the body of the loop, and it runs n times. One run through the body is called a pass (or an iteration).
REPEAT 3 TIMES
{
DISPLAY("Hello")
}The exam shows AP pseudocode. You run Python. Here is the same program in Python, ready to run:
Change the 3 to 5 and run it again. Compare the two forms:
| AP pseudocode | Python |
|---|---|
REPEAT 3 TIMES | for i in range(3): |
The body is inside { } | The body is indented under the : |
DISPLAY("Hello") shows Hello Hello Hello on one line | print("Hello") puts each Hello on a new line |
DISPLAY always adds a space after the value, so many DISPLAY calls make one line. The values are the same in both languages. Only the layout differs.
REPEAT 3 TIMES has no counter. Python's i goes 0, 1, 2 (it starts at 0), but AP gives you nothing like it. If you need a counter in AP, you build it yourself (next section). The number can also be a variable. REPEAT n TIMES with n ← 0 runs the body zero times.
Check your understanding
What is displayed when this code runs?
REPEAT 4 TIMES
{
DISPLAY("A")
}Counters and accumulators
A loop is most useful when each pass changes a variable. Two patterns appear everywhere:
- A counter goes up by the same amount on each pass (
n ← n + 1). - An accumulator collects a running total (
total ← total + n).
Trace this program. A trace table has one row per pass and one column per variable, so you can follow the values like the computer does.
total ← 0
n ← 1
REPEAT 5 TIMES
{
total ← total + n
n ← n + 1
}
DISPLAY(total)| Pass | total after the pass | n after the pass |
|---|---|---|
| (start) | 0 | 1 |
| 1 | 0 + 1 = 1 | 2 |
| 2 | 1 + 2 = 3 | 3 |
| 3 | 3 + 3 = 6 | 4 |
| 4 | 6 + 4 = 10 | 5 |
| 5 | 10 + 5 = 15 | 6 |
The program displays 15, which is 1 + 2 + 3 + 4 + 5. Notice that n ends at 6, not 5: it is raised one more time after the last number was added. In Python you can let the loop make the counter:
range(1, 6) gives 1, 2, 3, 4, 5. It starts at the first number and stops before the second. So range(5) gives 0 to 4, and range(1, 6) gives 1 to 5.
Check your understanding
What is displayed when this code runs?
total ← 0
REPEAT 3 TIMES
{
total ← total + 4
}
DISPLAY(total)Worked example: doubling
Each pass doubles a number. What is value at the end?
value ← 1
REPEAT 5 TIMES
{
value ← value * 2
}
DISPLAY(value)Pass by pass, value becomes 2, 4, 8, 16, 32. The program displays 32, which is 2⁵ (the number of values 5 bits can hold, from Unit 1). Count the passes first (5) and then apply the body that many times. The Python version, for i in range(5): value = value * 2, gives the same result.
Common mistakes
- Starting the accumulator in the wrong place.
total ← 0must come before the loop. If it is inside the body, it resets to 0 on every pass and you only see the last pass. - Putting
DISPLAYinside the loop when you want one answer. Inside the body it shows a value on every pass. After the closing brace it shows one value, at the end. - Mixing up the number of passes and the last value.
REPEAT 5 TIMESruns 5 passes. A counter that started at 1 ends at 6. - Expecting a counter in AP.
REPEAT n TIMESdoes not give youi. - Python's range stops early.
range(1, 5)is 1, 2, 3, 4. It never reaches 5.
On the exam
You will see a short REPEAT n TIMES loop and be asked what it displays or what a variable holds at the end. Make a trace table. Count the passes carefully, and check where the starting value is set and where DISPLAY is. You may also be asked to choose code that does a job, such as adding the same amount five times.
Key words
Iteration: repeating a block of code. Also the name for one pass through it.
Loop: code that repeats.
Body: the statements inside the loop's braces (or indented in Python).
Counter: a variable that goes up (or down) by the same amount on each pass.
Accumulator: a variable that builds up a total across passes.
Trace table: a table of variable values, one row per pass.