Iteration & Loops (College Board AP® Computer Science Principles): Revision Note
Iteration & loop types
What is iteration?
Iteration is a repeating portion of an algorithm
It repeats a specified number of times, or until a given condition is met
Iteration statements change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met
AP CSP uses two loop structures:
REPEAT n TIMESandREPEAT UNTIL
REPEAT n TIMES
Executes the loop body a fixed number of times, determined before the loop runs
Used when the number of repetitions is known in advance
REPEAT 3 TIMES
{
DISPLAY("Hello")
}
This displays "Hello" exactly three times
REPEAT UNTIL
Executes the loop body repeatedly until a condition becomes true
The condition is evaluated before each iteration
If the condition of a
REPEAT UNTILloop is already true when the loop starts, the body does not execute at all, the condition is checked before each iteration, including the first oneUsed when the number of repetitions depends on a changing value or user input
count ← 1
REPEAT UNTIL(count > 5)
{
DISPLAY(count)
count ← count + 1
}
This displays 1 through 5, then stops when
countexceeds 5
Loop type | When to use | Number of iterations |
|---|---|---|
| Number of repetitions is known in advance | Fixed |
| Repetitions depend on a condition | Variable |
Loop behavior
How does loop termination work?
A loop terminates (stops) when its stopping condition is met
REPEAT n TIMESterminates automatically after exactlyniterationsREPEAT UNTILterminates when its condition evaluates totrue
Infinite loops
An infinite loop occurs when the condition of a
REPEAT UNTILloop never becomestrueThe program appears to freeze and continues looping indefinitely
Common causes:
The condition variable is never updated inside the loop body
The condition is logically impossible to satisfy
count ← 1
REPEAT UNTIL(count = 0)
{
count ← count + 1
}
This loop is infinite because
countincreases each iteration and will never equal0Every loop must have a clear, reachable stopping condition; if the condition variable never changes inside the loop body, the loop will run indefinitely
Examiner Tips and Tricks
REPEAT UNTILstops when the condition istrue, notfalse— read the condition carefully in code trace questions. ForREPEAT n TIMES, the loop body always runs exactlyntimes; nothing inside the body can cut it short. In infinite loop questions, check whether the condition variable actually changes inside the loop body — if it does not, the loop will never terminateFor the AP Create Performance Task, iteration is commonly used to process lists — if your program uses a list, a loop makes your solution more efficient and generalizable
Worked Example
What will the following code segment display?
x ← 1
REPEAT UNTIL(x > 3)
{
DISPLAY(x)
x ← x + 1
}
(A) 1 2 3 4
(B) 1 2 3
(C) 2 3 4
(D) The loop runs infinitely
[1]
Answer:
(B) 1 2 3 [1 mark]
The condition is checked before each iteration. Starting from
x = 1: display 1, increment to 2. Display 2, increment to 3. Display 3, increment to 4. Now the conditionx > 3is true, so the loop terminates before displaying 4.
Unlock more, it's free!
Was this revision note helpful?