---
title: "Iteration | FiveStudy"
description: "How REPEAT n TIMES and REPEAT UNTIL work, why the condition is checked before every pass, and how to trace a loop without losing count."
url: https://fivestudy.com/guides/ap-csp-3-8-iteration
---

[AP Computer Science Principles](https://fivestudy.com/ap-computer-science-principles) · Study guide

# Iteration

[Figure: FiveStudy]

FiveStudy

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

# Iteration

How a program repeats steps: loops that run a set number of times, loops that run until a condition is true, and how to trace both without losing count.

1. What iteration is 2. REPEAT n TIMES 3. REPEAT UNTIL 4. Zero passes and endless loops 5. Tracing a loop 6. Loops inside loops 7. Worked examples 8. Common mistakes

## 1 What iteration is

**Iteration** is a repeating portion of an algorithm. It repeats either a **set number of times** (“stir 20 times”) or **until a condition is met** (“stir until the sugar dissolves”). In the first kind you know the count before you start. In the second, the count is whatever it turns out to be, and it can even be zero.

Plain-language algorithms hide loops in words like “for each”, “keep”, “until”, and “while there are”. Find the part that repeats and the rule that stops it:

| Algorithm in words | Part that repeats | What stops it |
| --- | --- | --- |
| Fill the sink. While there is a dish in the sink, wash one dish and put it on the rack. Drain the sink. | wash one dish, rack it | no dishes left (a condition) |
| Open the attendance list. For each name, read it out and mark present or absent. | read a name, mark it | every name done (a known count) |
| Keep flipping a coin until it lands on heads. | flip the coin | heads shows (a condition) |

“Fill the sink” and “drain the sink” happen once each. Only the middle step repeats, and if the sink starts empty it repeats zero times. That is not a bug; it is what “while there is a dish” means when there are no dishes.

**Figure 1.** Each loop asks its question first. REPEAT n TIMES counts passes; REPEAT UNTIL leaves the first time its condition is true.

## 2 REPEAT n TIMES

Iteration statements change the normal top-to-bottom order of a program by running a block of statements again and again until a stopping condition is met. The first loop in the reference language counts:

Reference language

```
REPEAT n TIMES
{
  <block of statements>
}
```

The block runs **n times**, no more and no less. Here a loop adds the numbers 1 through 4:

```
total ← 0
n ← 1
REPEAT 4 TIMES
{
  total ← total + n
  n ← n + 1
}
DISPLAY(total)
```

| Pass | total before | n before | total after | n after |
| --- | --- | --- | --- | --- |
| 1 | 0 | 1 | 1 | 2 |
| 2 | 1 | 2 | 3 | 3 |
| 3 | 3 | 3 | 6 | 4 |
| 4 | 6 | 4 | 10 | 5 |

After the fourth pass the loop is done, so the program displays `10`. The variable `n` is just a regular variable that the block changes; the loop itself keeps its own count of passes.

## 3 REPEAT UNTIL

Reference language

```
REPEAT UNTIL (condition)
{
  <block of statements>
}
```

The block repeats **until** the Boolean expression `condition` is true. The condition is checked **before every pass**, including the first. The loop ends the first time the check comes out true.

```
k ← 1
REPEAT UNTIL (k > 8)
{
  k ← k * 2
}
DISPLAY(k)
```

**Figure 2.** The condition is checked 5 times but the block runs only 4 times: the last check is the one that ends the loop.

The program displays `16`, not 8. When `k` is 8, the check `8 > 8` is false, so the block runs once more and doubles `k` to 16. Counting checks and passes separately is what keeps a trace accurate: for `REPEAT UNTIL` there is always one more check than there are passes.

## 4 Zero passes and endless loops

Because the condition is checked before the block, two edge cases follow.

**Zero passes.** If the condition is already true at the first check, the block never runs:

```
x ← 12
REPEAT UNTIL (x ≥ 10)
{
  x ← x + 3
}
DISPLAY(x)
```

The check `12 ≥ 10` is true right away, so the program displays `12` and the line `x ← x + 3` is never executed.

**Endless (infinite) loop.** If the condition can never become true, the loop never ends:

```
n ← 5
REPEAT UNTIL (n = 0)
{
  n ← n - 2
}
```

`n` goes 5, 3, 1, -1, -3, and keeps falling. It jumps over 0, so `n = 0` is never true. When you write a `REPEAT UNTIL` loop, ask: does the block move the variables toward making the condition true, and can it reach it, not skip past it?

The key idea

`REPEAT n TIMES` runs its block n times. `REPEAT UNTIL (condition)` checks the condition before each pass and stops the first time it is true, so the block can run zero times, and runs forever if the condition can never become true.

## 5 Tracing a loop

To find what a loop does, make a table with one row per check (for `REPEAT UNTIL`) or per pass (for `REPEAT n TIMES`), and a column for every variable that changes. Fill in the row, then do what the row says. Don't skip rows in your head; most wrong answers are one pass too many or too few.

The two loops can do the same job. A `REPEAT UNTIL` with a counter behaves like `REPEAT 3 TIMES`:

```
i ← 0
REPEAT UNTIL (i = 3)
{
  DISPLAY("hi")
  i ← i + 1
}
```

Both versions display `hi` three times. The counter starts at 0, goes up by 1 each pass, and the loop stops when it reaches 3. Start the counter at 1 instead and you get only two passes.

## 6 Loops inside loops

A loop's block can contain another loop. The inner loop then runs all the way through on *every* pass of the outer loop:

```
count ← 0
REPEAT 3 TIMES
{
  REPEAT 4 TIMES
  {
    count ← count + 1
  }
}
DISPLAY(count)
```

The outer loop makes 3 passes, and each pass runs the inner block 4 times, so `count` goes up 3 × 4 = 12 times. Put the same two loops one after the other instead, and the counts add:

```
count ← 0
REPEAT 3 TIMES
{
  count ← count + 1
}
REPEAT 4 TIMES
{
  count ← count + 1
}
DISPLAY(count)
```

This version displays `7`, which is 3 + 4. A quick way to remember it: nesting multiplies, one after the other adds.

## 7 Worked examples

Worked Example 1 Count the passes

### Subtract until small

What is displayed?

```
count ← 0
value ← 50
REPEAT UNTIL (value < 10)
{
  value ← value - 15
  count ← count + 1
}
DISPLAY(count)
DISPLAY(value)
```

1. 1

   Make one row per check of `value < 10`:

   | Check | value | value < 10? | What happens |
   | --- | --- | --- | --- |
   | 1 | 50 | false | subtract 15, add 1 to count |
   | 2 | 35 | false | subtract 15, add 1 to count |
   | 3 | 20 | false | subtract 15, add 1 to count |
   | 4 | 5 | true | stop |

2. 2

   The block ran 3 times, so `count` is 3. The last value of `value` is 5.

**Answer:** `3` then `5`. Four checks, three passes.

Worked Example 2 Write the loop

### Saving up

Mina has $200 saved and adds $45 each week. Write a loop that finds how many weeks until she has at least $500, and say what it displays.

1. 1

   The number of weeks is not known in advance, so use `REPEAT UNTIL`.

2. 2

   Stop when `balance ≥ 500`; each pass adds 45 and counts one week.

   ```
   balance ← 200
   weeks ← 0
   REPEAT UNTIL (balance ≥ 500)
   {
     balance ← balance + 45
     weeks ← weeks + 1
   }
   DISPLAY(weeks)
   ```

3. 3

   Balances at each check: 200, 245, 290, 335, 380, 425, 470, 515. The check first comes out true at 515.

**Answer:** It displays `7`: after 7 weeks she has $515.

Worked Example 3 Compare segments

### Which displays 1 2 4 8?

Which of these segments display `1 2 4 8`?

Segment A

```
x ← 1
REPEAT 4 TIMES
{
  DISPLAY(x)
  x ← x * 2
}
```

Segment B

```
x ← 1
REPEAT 4 TIMES
{
  x ← x * 2
  DISPLAY(x)
}
```

Segment C

```
x ← 1
REPEAT UNTIL (x > 8)
{
  DISPLAY(x)
  x ← x * 2
}
```

1. 1

   A displays before doubling: `1 2 4 8`.

2. 2

   B doubles before displaying, so the first value shown is 2: `2 4 8 16`.

3. 3

   C displays while `x` is 1, 2, 4, 8; when `x` becomes 16 the check `16 > 8` is true and it stops: `1 2 4 8`.

**Answer:** A and C. The order of statements inside the block matters as much as the loop type.

Worked Example 4 Fix the loop

### Odd numbers forever

This segment should display the odd numbers from 1 to 9, but it never stops. Why, and how do you fix it?

```
count ← 1
REPEAT UNTIL (count = 10)
{
  DISPLAY(count)
  count ← count + 2
}
```

1. 1

   `count` takes the values 1, 3, 5, 7, 9, 11, … It is always odd, so it is never equal to 10.

2. 2

   The condition can never be true, so the loop is endless.

3. 3

   Change the condition to one the loop cannot jump over:

   ```
   count ← 1
   REPEAT UNTIL (count > 10)
   {
     DISPLAY(count)
     count ← count + 2
   }
   ```

**Answer:** With `count > 10` the segment displays `1 3 5 7 9` and stops.

## 8 Common mistakes

Common Mistake 1 · Stopping one pass early

For `REPEAT UNTIL (k > 8)`, the loop does not stop at 8: `8 > 8` is false. Check the condition with the actual value, every time.

Common Mistake 2 · Reading UNTIL as WHILE

`REPEAT UNTIL (x = 5)` keeps going while `x` is *not* 5. It stops when the condition becomes true, not when it becomes false.

Common Mistake 3 · Forgetting the zero case

If the condition is true at the first check, or n is 0 in `REPEAT n TIMES`, the block never runs and every variable keeps its starting value.

Common Mistake 4 · A condition the loop can skip

Counting by 2 from 1 never hits 10, and counting down by 2 from 5 never hits 0. Use `>`, `≥`, `<`, or `≤` when a variable moves in steps bigger than 1.

Explain a loop like this

“The loop repeats until `value < 10`. The condition is checked before each pass: it is false at 50, 35, and 20, so the block runs three times, and true at 5, so the loop stops. The segment displays 3 and then 5.”

Name the stopping rule, give the value at each check, and say how many times the block ran.

FiveStudy AP® Computer Science Principles · Iteration
