---
title: "AP CSP 3.8: Iteration and Loops"
description: "Review AP CSP 3.8 iteration, including REPEAT n TIMES, REPEAT UNTIL, stopping conditions, zero-iteration cases, infinite loops, and exam pseudocode."
canonical: "https://fiveable.me/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR"
type: "study-guide"
subject: "AP Computer Science Principles"
unit: "Unit 3 – Algorithms & Programming Fundamentals"
lastUpdated: "2026-06-07"
---

# AP CSP 3.8: Iteration and Loops

## Summary

Review AP CSP 3.8 iteration, including REPEAT n TIMES, REPEAT UNTIL, stopping conditions, zero-iteration cases, infinite loops, and exam pseudocode.

## Guide

## TLDR
Iteration means repeating a block of code more than once. In [AP Computer Science Principles](/ap-comp-sci-p "fv-autolink"), you mainly work with two loop types from the exam reference sheet: `REPEAT n TIMES`, which runs a set number of times, and `REPEAT UNTIL (condition)`, which keeps running until a [condition](/ap-comp-sci-p/key-terms/condition "fv-autolink") becomes true.

## Why This Matters for the AP Computer Science Principles Exam

Iteration is one of the three building blocks of every [algorithm](/ap-comp-sci-p/key-terms/algorithm "fv-autolink"), along with [sequencing](/ap-comp-sci-p/key-terms/sequencing "fv-autolink") and selection. Expect to trace loops and predict their output or side effects on multiple-choice questions, and to describe iteration without using a specific programming language. Loops also show up constantly in the Create performance task, since most useful programs repeat actions, and you may need to explain how a loop in your program works in your written responses.

Knowing the exam [pseudocode](/ap-comp-sci-p/key-terms/pseudocode "fv-autolink") for loops matters because the multiple-choice section can be written in that pseudocode, and you should be able to read it the same way no matter what language you used in class.

## Key Takeaways

- Iteration is a repeating portion of an algorithm that runs a set number of times or until a condition is met.
- `REPEAT n TIMES` runs the loop body exactly n times. The n can be a number or a [variable](/ap-comp-sci-p/key-terms/variable "fv-autolink") holding a number.
- `REPEAT UNTIL (condition)` checks the condition first and keeps repeating the body until that condition becomes true.
- A loop can run zero times. If a `REPEAT UNTIL` condition is already true at the start, the body never executes.
- An infinite loop happens when the stopping condition never becomes true, so something inside the loop must move it toward stopping.
- You can build loop conditions with relational and [logical operators](/ap-comp-sci-p/unit-3/boolean-expressions/study-guide/LkLTi80KQM04AnmNBxCq "fv-autolink") like `AND` and `OR`.

## REPEAT n TIMES

A count-controlled loop runs a fixed number of times. On the exam reference sheet this is written as:

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

The block of statements runs exactly n times. The value of n can be a literal number or a variable that holds a number. You might see it written directly as `REPEAT 5 TIMES`, but it often uses a variable:

```
n ← 5
REPEAT n TIMES
{
    DISPLAY(n)
}
```

If you use Python in class, the closest match is a `for ... in range` loop:

```python
for x in range (0, 6):
    print (x)

    # The range includes 0 but not 6, so this loop runs 5 times
    # This loop can also be written as...

for x in range (6):
    print (x)
```

Use `REPEAT n TIMES` when you already know how many repetitions you need.

## REPEAT UNTIL (condition)

A condition-controlled loop keeps running until its condition becomes true. On the exam reference sheet this is written as:

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

The body repeats until the Boolean expression in the condition evaluates to true. Two rules are worth memorizing:

- The condition is checked before the body runs. If the condition is already true at the start, the body runs zero times.
- An infinite loop occurs when the condition never becomes true, so the loop never stops.

In Python, the closest tool is a `while` loop, but be careful: a `while` loop runs *while* its condition is true, which is the opposite of *until*.

```python
tacos = 5
while tacos > 0:
    print ("Nom Nom")
    tacos = tacos - 1

# Note: tacos = tacos - 1 can also be written as tacos -= 1.

# The same goes for addition (tacos += 1)
```

How many times will this print "Nom Nom"? Five times. Each pass lowers `tacos` by one until it reaches 0.

Notice the `tacos = tacos - 1` line. That update is what moves the loop toward stopping. Without it, `tacos` would stay at 5 forever and you would get an infinite loop. A common cause of bugs is forgetting to change the variable that the condition depends on.

Here is the zero-iterations case in action. The body never runs because the stopping condition is already met:

```python
tacos = 0
while tacos > 0:
    print ("Nom Nom")
    tacos = tacos - 1

```

## Matching while Loops to REPEAT UNTIL

Because `while` checks "keep going while true" and `REPEAT UNTIL` checks "stop when true," you can flip one into the other using `NOT`. This example keeps running until `tacos` equals 0, which lines up with `REPEAT UNTIL` thinking:

```python
tacos = 5
while not tacos == 0:
    print ("Nom Nom")
    tacos = tacos - 1

```

When you compare the two styles, focus on the stopping point: `REPEAT UNTIL (tacos = 0)` and `while not tacos == 0` end at the same moment.

## Using AND and OR in Loop Conditions

Loop conditions are Boolean expressions, so you can combine comparisons with `AND` and `OR`.

With `AND`, every part must be true for the loop to keep going:

```python
tacos = 5
avocados = 5

while tacos > 0 and avocados > 0:
    print ("Nom Nom")
    tacos = tacos - 1
    avocados = avocados - 1

```

With `OR`, the loop keeps going as long as at least one part is true:

```python
tacos = 5
quesadillas = 10
while tacos > 0 or quesadillas > 0:
    print ("Nom Nom")
    tacos = tacos - 1
    quesadillas = quesadillas - 1

# this loop prints "Nom Nom" 10 times, because it keeps going
# until both tacos and quesadillas reach 0
```

Trace these slowly. With `OR`, the loop only stops once both conditions are false, which is why the second example runs longer than the first.

## How to Use This on the AP Computer Science Principles Exam

### MCQ

- Trace loops one pass at a time. Write down each variable's value after every iteration so you do not lose track.
- Count carefully. `REPEAT n TIMES` runs exactly n times, but `REPEAT UNTIL` depends on when the condition flips to true.
- Always check the zero-iterations case. If a `REPEAT UNTIL` condition starts out true, the body never runs.
- Watch for off-by-one errors, where a loop runs one too many or one too few times.

### Code Tracing

- Identify the stopping condition first, then find the line that changes the variable in that condition.
- If nothing inside the loop changes the condition's variable, suspect an infinite loop.
- For `AND` and `OR` conditions, decide what makes the whole condition false, since that is when the loop stops.

### Create Performance Task

- Use a loop when your program repeats an action, and be ready to explain in plain language what the loop does and why it is there.
- Make sure your loop actually reaches its stopping condition so it does not run forever.

## Common Misconceptions

- "REPEAT UNTIL runs while the condition is true." It is the opposite. The loop runs until the condition becomes true, then stops.
- "A loop always runs at least once." Not true. A `REPEAT UNTIL` loop checks its condition first, so it can run zero times.
- "n in REPEAT n TIMES has to be a plain number." It can be a variable that holds a number, not just a literal value.
- "Infinite loops only happen from typos." They usually happen because nothing inside the loop moves the condition toward stopping.
- "Python while loops and REPEAT UNTIL are the same thing." They stop on opposite conditions. A `while` loop continues while true; `REPEAT UNTIL` stops when true.
- "Using AND makes a loop run longer than OR." With `AND` the loop stops as soon as any part is false, so it often stops sooner than an `OR` version.

## Related AP Computer Science Principles Guides

- [3.1 Variables and Assignments](/ap-comp-sci-p/unit-3/variables-assignments/study-guide/vtJhAf5XFOkm1uHNDMvh)
- [Big Idea 3: Algorithms and Programming](/ap-comp-sci-p/unit-3/review/study-guide/eOWMqAJUdtnmttaCSlis)
- [3.12 Calling Procedures](/ap-comp-sci-p/unit-3/calling-procedures/study-guide/lwdr3yhVOtUJZhAmJ5cu)
- [3.18 Undecidable Problems](/ap-comp-sci-p/unit-3/undecidable-problems/study-guide/q0SSR2ddayx397Hy6ztA)
- [3.17 Algorithmic Efficiency](/ap-comp-sci-p/unit-3/algorithmic-efficiency/study-guide/jGSWIqW49BtrQ8dqCWFd)
- [3.2 Data Abstraction](/ap-comp-sci-p/unit-3/data-abstraction/study-guide/kMMTClSiHohfiaHMGFFE)

## Vocabulary

- **Boolean expression**: An expression that evaluates to either true or false, often composed of conditions combined with logical operators.
- **REPEAT UNTIL(condition)**: An iteration statement that repeats a block of statements until a Boolean condition evaluates to true.
- **REPEAT n TIMES**: An iteration statement that executes a block of statements exactly n times.
- **algorithm**: Step-by-step procedures or sets of rules designed to solve a problem or accomplish a task.
- **condition**: A statement that evaluates to either true or false, used to determine the flow of execution in a selection structure.
- **infinite loop**: A loop that never terminates because the ending condition will never evaluate to true.
- **iteration**: A repeating portion of an algorithm that executes a specified number of times or until a given condition is met.
- **iteration statements**: Programming statements that repeat a block of code zero or more times until a stopping condition is met.
- **loop body**: The block of statements that is repeated within an iteration statement.
- **sequential flow of control**: The order in which statements are executed in a program; conditional statements can alter this flow by executing different code based on conditions.
- **stopping condition**: A Boolean expression that, when true, causes an iteration loop to terminate.

## FAQs

### What is iteration in AP Computer Science Principles?

Iteration is a repeating part of an algorithm. It repeats a block of statements a set number of times or until a stopping condition is met.

### What does REPEAT n TIMES do?

REPEAT n TIMES runs the block of statements exactly n times. The value n can be a number or a variable containing a number.

### What does REPEAT UNTIL(condition) do?

REPEAT UNTIL(condition) repeats the block until the condition evaluates to true. If the condition is true at the start, the loop body runs zero times.

### What causes an infinite loop in AP CSP?

An infinite loop happens when the stopping condition never becomes true. Usually that means the loop does not update the variable or state that the condition depends on.

### How do you trace iteration on the AP CSP exam?

Track each variable after every pass through the loop, check the stopping condition carefully, and watch for off-by-one errors or cases where the body runs zero times.

### How is AP CSP 3.8 tested?

AP CSP 3.8 often asks you to express an algorithm with iteration, determine the result or side effect of a loop, and reason about REPEAT n TIMES or REPEAT UNTIL pseudocode.

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR#what-is-iteration-in-ap-computer-science-principles","name":"What is iteration in AP Computer Science Principles?","acceptedAnswer":{"@type":"Answer","text":"Iteration is a repeating part of an algorithm. It repeats a block of statements a set number of times or until a stopping condition is met."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR#what-does-repeat-n-times-do","name":"What does REPEAT n TIMES do?","acceptedAnswer":{"@type":"Answer","text":"REPEAT n TIMES runs the block of statements exactly n times. The value n can be a number or a variable containing a number."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR#what-does-repeat-untilcondition-do","name":"What does REPEAT UNTIL(condition) do?","acceptedAnswer":{"@type":"Answer","text":"REPEAT UNTIL(condition) repeats the block until the condition evaluates to true. If the condition is true at the start, the loop body runs zero times."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR#what-causes-an-infinite-loop-in-ap-csp","name":"What causes an infinite loop in AP CSP?","acceptedAnswer":{"@type":"Answer","text":"An infinite loop happens when the stopping condition never becomes true. Usually that means the loop does not update the variable or state that the condition depends on."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR#how-do-you-trace-iteration-on-the-ap-csp-exam","name":"How do you trace iteration on the AP CSP exam?","acceptedAnswer":{"@type":"Answer","text":"Track each variable after every pass through the loop, check the stopping condition carefully, and watch for off-by-one errors or cases where the body runs zero times."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/iteration/study-guide/7megnIMaNHzDdrVKT9mR#how-is-ap-csp-38-tested","name":"How is AP CSP 3.8 tested?","acceptedAnswer":{"@type":"Answer","text":"AP CSP 3.8 often asks you to express an algorithm with iteration, determine the result or side effect of a loop, and reason about REPEAT n TIMES or REPEAT UNTIL pseudocode."}}]}
```
