01 / 21

Iteration
Loops in Action

Master the power of repeating instructions with loops: from counting through lists to running code until a condition is met.

AP Computer Science Principles

What Is Iteration?

Iteration is the process of repeating a set of instructions multiple times. Instead of writing the same code over and over, a loop lets you execute a block of code as many times as needed: a fixed number of times, once for each item in a list, or until a condition changes.

Why Iteration Matters

Efficiency
Process millions of data points without writing millions of lines of code.
Automation
Repeat tedious tasks automatically: sending emails, checking sensors, rendering frames.
Scalability
The same loop works whether you have 10 items or 10 million items.

Anatomy of a Loop

Initialize Condition true? Yes Loop Body Repeat No Continue Program

Two Core Types of Loops

Count-Controlled
Repeats a known number of times. Uses a counter variable: REPEAT n TIMES or FOR EACH item in a list.
Condition-Controlled
Repeats until a condition changes. Uses a Boolean test: REPEAT UNTIL (condition is true). The number of repetitions may not be known in advance.

REPEAT n TIMES

When you know exactly how many times to repeat, use REPEAT n TIMES. The loop body executes \( n \) times, then stops.

REPEAT 3 TIMES { DISPLAY("Hello!") }

FOR EACH Item in a List

When you want to process every element of a list, use FOR EACH. The loop variable takes on each value in turn.

scores ← [85, 92, 78, 96] total ← 0 FOR EACH s IN scores { total ← total + s } DISPLAY(total)

REPEAT UNTIL (Condition)

When you don't know in advance how many repetitions are needed, use REPEAT UNTIL. The loop checks its condition before each iteration and stops when the condition becomes true.

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

REPEAT n TIMES vs REPEAT UNTIL

REPEAT n TIMES REPEAT UNTIL
Iterations known? Yes: fixed count No: depends on data
Controlled by A counter \( n \) A Boolean condition
Stops when Counter reaches \( n \) Condition becomes true
Risk of infinite loop? No Yes: if condition never becomes true
Example use Draw 5 shapes Search until found

How to Trace a Loop

1
Write down all variables and their initial values before the loop starts.
2
Check the condition. For REPEAT n TIMES, check if the counter has reached \( n \). For REPEAT UNTIL, evaluate the Boolean expression.
3
Execute the loop body line by line. Update each variable on your trace table.
4
Return to step 2. Repeat until the loop terminates.
5
Read the final values of all variables after the loop. That's your answer.

Example 1: Counting with a Loop

sum ← 0 i ← 1 REPEAT 4 TIMES { sum ← sum + i i ← i + 1 }
Iteration i (before) sum (after) i (after)
1st112
2nd233
3rd364
4th4105

Example 2: Filtering a List

temps ← [68, 74, 55, 80, 62] hot ← [] FOR EACH t IN temps { IF(t > 70) { APPEND(hot, t) } }

Example 3: REPEAT UNTIL Search

nums ← [3, 7, 2, 9, 5] index ← 1 found ← false REPEAT UNTIL(found OR index > LENGTH(nums)) { IF(nums[index] = 9) { found ← true } index ← index + 1 }

Nested Loops

A loop inside another loop is called a nested loop. The inner loop completes all its iterations for each single iteration of the outer loop.

REPEAT 3 TIMES ← outer loop { REPEAT 2 TIMES ← inner loop { DISPLAY("*") } }

Common Mistakes

✕ Infinite Loop
Forgetting to update the variable in the condition. If the condition never becomes true, the loop runs forever.
✕ Off-by-One Error
Looping one time too many or too few. Check: does the condition use < or <= ? Does the counter start at 0 or 1?
✕ Wrong Scope
Placing code inside the loop that should be outside (or vice versa). Resetting a sum inside the loop erases previous work.

Iteration Summary

Construct Syntax When to Use
Fixed count REPEAT n TIMES Known number of repetitions
List traversal FOR EACH item IN list Process every element
Condition-based REPEAT UNTIL(cond) Unknown iterations, stop on event
Nested loops Loop inside a loop 2D patterns, all pairs, grids

Question 1 of 5

What is the value of x after this code runs?

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

Question 2 of 5

What does this code display?

n ← 1 REPEAT UNTIL(n > 3) { DISPLAY(n) n ← n + 1 }

Question 3 of 5

Which of the following will cause an infinite loop?

Question 4 of 5

What is the value of result after this code?

data ← [4, 1, 7, 3] result ← data[1] FOR EACH d IN data { IF(d > result) { result ← d } }

Question 5 of 5

How many times does DISPLAY execute in this nested loop?

REPEAT 4 TIMES { REPEAT 3 TIMES { DISPLAY("go") } }