6m left·0%
Reading Time: 6 min
Last Updated: March 30, 2026
Main Ideas: 6
Reading Time: 6 min
Last Updated: March 30, 2026
Main Ideas: 6

Topic 3.8 Notes – Iteration

Verified for 2027 AP® Computer Science Principles Exam
Read aloud
Iteration is how programs repeat actions without rewriting the same code over and over. In AP CSP, iteration means a loop that runs either a specific number of times or until a condition becomes true. Understanding how loops change program flow and how variables update inside them is essential for tracing code and writing your own algorithms.

What Iteration Is

Iteration is a repeating portion of an algorithm. Instead of executing strictly top to bottom, the program loops back and runs a block of statements again.

A loop can run:

  • A fixed number of times
  • Until a Boolean condition becomes true

So iteration changes the normal sequential flow of control. The program keeps returning to the top of the loop block until the stopping rule is satisfied.

On quizzes and the AP exam, you’ll need to:

  • Describe an algorithm that uses repetition in plain language
  • Write loops using the exact exam reference sheet format
  • Trace loops to determine final values or outputs

A loop can execute zero or more times. That “zero” part matters more than students expect.

REPEAT n TIMES

This loop runs a set number of times.

Reference sheet format:

REPEAT n TIMES {
   <block of statements>
}
  • n can be a number or a variable.
  • The block executes exactly n times.
  • If n = 0, the loop does not run at all.

Example

sum ← 0
REPEAT 4 TIMES {
   sum ← sum + 3
}
DISPLAY(sum)

Trace it:

  • Start: sum = 0
  • After 1st loop: 3
  • After 2nd: 6
  • After 3rd: 9
  • After 4th: 12

Final output is 12.

This is a counter or accumulator pattern. The loop runs a known number of times, and something changes each time.

Students often forget that the loop does not automatically change anything. Only the statements inside the block change variables.

REPEAT UNTIL(condition)

This loop continues until the condition becomes true.

Reference sheet format:

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

Here’s the key idea:

  • The loop runs while the condition is false.
  • The moment the condition becomes true, the loop stops.
  • If the condition is already true at the beginning, the loop runs zero times.

Example

x ← 1
REPEAT UNTIL(x > 5) {
   DISPLAY(x)
   x ← x + 2
}

Trace it:

  • Start: x = 1 (1 > 5 is false → run loop)
  • Print 1 → x becomes 3
  • 3 > 5? false → run
  • Print 3 → x becomes 5
  • 5 > 5? false → run
  • Print 5 → x becomes 7
  • 7 > 5? true → stop

Output:

1
3
5

Notice that 7 is never printed.

Infinite Loops

An infinite loop happens when the stopping condition never becomes true.

Common causes:

  • The variable in the condition never changes.
  • The variable changes in the wrong direction.
  • The condition can never logically become true.

Example of a problem:

count ← 5
REPEAT UNTIL(count = 0) {
   DISPLAY(count)
}

count never changes. The condition never becomes true. The loop never stops.

When tracing on a test, always ask:

  • What variable affects the condition?
  • Does it change inside the loop?
  • Is it moving toward making the condition true?

Using Boolean Expressions in Conditions

Conditions can include:

  • Relational operators: =, ≠, <, >, ≤, ≥
  • Logical operators: AND, OR, NOT

Example with AND:

a ← 4
b ← 3
REPEAT UNTIL(a = 0 AND b = 0) {
   a ← a - 1
   b ← b - 1
}

The loop stops only when both values are 0.

With OR, the condition becomes true sooner. Students often misread this. Slow down and evaluate the full Boolean expression each time.

Expressing Iteration Without Code

You might be asked to describe a loop in words.

Example:

“Add 2 to a total five times.”

That translates to:

total ← 0
REPEAT 5 TIMES {
   total ← total + 2
}

Or:

“Keep asking for input until the user enters a positive number.”

That translates to:

REPEAT UNTIL(num > 0) {
   num ← INPUT()
}

The exam cares that you clearly describe:

  • What repeats
  • When it stops

Key Takeaways

A loop can run zero times if its stopping condition is already true.
REPEAT n TIMES runs exactly n times, no matter what happens inside.
REPEAT UNTIL(condition) runs while the condition is false.
Infinite loops happen when the stopping condition never becomes true.
When tracing, track how the variable in the condition changes each iteration.
AND requires both parts to be true to stop; OR stops when either part is true.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining