Fiveable

⌨️AP Computer Science Principles Unit 3 Review

QR code for AP Computer Science Principles practice questions

3.8 Iteration

3.8 Iteration

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
⌨️AP Computer Science Principles
Unit & Topic Study Guides

AP Computer Science Principles Exam

Pep mascot

TLDR

Iteration means repeating a block of code more than once. In AP Computer Science Principles, 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 becomes true.

Why This Matters for the AP Computer Science Principles Exam

Iteration is one of the three building blocks of every algorithm, along with sequencing 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 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 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 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:

</>Code
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:

</>Code
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:

</>Code
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.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

algorithm

Step-by-step procedures or sets of rules designed to solve a problem or accomplish a task.

Boolean expression

An expression that evaluates to either true or false, often composed of conditions combined with logical operators.

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.

REPEAT n TIMES

An iteration statement that executes a block of statements exactly n times.

REPEAT UNTIL(condition)

An iteration statement that repeats a block of statements until a Boolean condition evaluates to true.

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.

Frequently Asked Questions

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.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot