Topic 3.8: Iteration | AP CSP Big Idea 3 | APCSExamPrep.com
Iteration
After this lesson, you will be able to:
- Distinguish definite iteration (REPEAT n TIMES, for range) from indefinite iteration (REPEAT UNTIL, while)
- Know that range(n) runs exactly n times with the loop variable going 0 to n minus 1
- Build counter and accumulator patterns and read their final value after the loop
- Translate REPEAT UNTIL to a Python while by negating the condition
- Recognize when a loop body runs zero times and when a loop becomes infinite
- Describe a meaningful loop with iteration for your Create Performance Task
A program that greets one user is easy. A program that greets a thousand users, or keeps asking for a password until it is correct, needs to repeat itself. That repetition is iteration. Sometimes you know the exact count ahead of time, like sending five reminder emails. Sometimes you do not, like rolling a die until you get a six. This lesson is about controlling exactly how many times your code runs, and never one time too many or too few.
What Iteration Means
Iteration is the repetition of a portion of an algorithm. Instead of writing the same instruction over and over, you write it once and tell the program how many times, or under what condition, to repeat it. Iteration is one of the three fundamental building blocks of every algorithm, alongside sequencing and selection, and it is exactly what the Create Performance Task rubric looks for.
There are two flavors. Definite iteration repeats a fixed, known number of times. In AP pseudocode this is REPEAT n TIMES, and in Python it is for i in range(n). Indefinite iteration repeats until some condition is met, and you do not know in advance how many passes that will take. That is REPEAT UNTIL(condition) in pseudocode and while in Python.
for i in range(3): print("hi")
REPEAT 3 TIMES { DISPLAY("hi") }
The loop above runs exactly three times because range(3) produces the values 0, 1, and 2. The important idea for definite iteration: range(n) runs the body n times, but the loop variable takes the values 0 through n minus 1, not 1 through n. That gap is the source of the most common loop bug on the exam.
The CED (AAP-2.J and AAP-2.K) requires you to read and write both loop types. Memorize the mapping: REPEAT n TIMES equals for i in range(n), and REPEAT UNTIL(cond) equals while not cond. A definite loop always runs a countable number of times; an indefinite loop runs an unknown number of times.
for i in range(4): print("x")
Counters and Accumulators
Two patterns power almost every useful loop. A counter is a variable that goes up by one each pass to tally how many times something happened. An accumulator is a variable that keeps a running total, updated on every pass, so that by the end it holds the combined result of the whole loop.
The recipe is always the same: set the variable to a starting value before the loop, update it inside the loop, and read it after the loop. Forgetting the initialization, or putting the final print inside the loop instead of after it, are classic mistakes.
total = 0 for i in range(1, 6): total = total + i print(total)
total <- 0 i <- 1 REPEAT 5 TIMES { total <- total + i i <- i + 1 } DISPLAY(total)
This loop adds 1, then 2, then 3, 4, and 5 into total, so it prints 15. Notice the Python range(1, 6) stops before 6, giving the values 1 through 5. The pseudocode reaches the same result with REPEAT 5 TIMES and its own counter. Both are definite iteration; they run a fixed five times.
Exam questions love to ask for the value of the accumulator or the loop counter after the loop ends, not during it. Trace every pass, then report the value once the loop has finished. Do not stop tracing one pass too early.
REPEAT UNTIL Is the Opposite of while
This is the single most tested idea in this topic, and it trips up students every year. A Python while loop keeps going while its condition is true and stops when the condition becomes false. An AP REPEAT UNTIL loop is the mirror image: it keeps going until its condition becomes true, checking after nothing and stopping the moment the condition is true.
Because they are opposites, translating between them means negating the condition. A loop that should continue while count < 3 in Python becomes REPEAT UNTIL (count >= 3) in pseudocode. Same behavior, flipped condition.
count = 0 while count < 3: print(count) count = count + 1
count <- 0 REPEAT UNTIL (count >= 3) { DISPLAY(count) count <- count + 1 }
Both loops above print 0, 1, 2 and then stop. The Python version continues while count < 3 is true. The pseudocode version repeats until count >= 3 is true. Read the two conditions and confirm they are logical opposites of each other. If you ever see a REPEAT UNTIL and want to think in Python, mentally rewrite it as while not (condition).
If a question gives you a REPEAT UNTIL (x > 10) and you read it as if it were a Python while x > 10, you will get the loop backwards and pick the wrong answer every time. UNTIL means stop when true. WHILE means continue when true. They are not the same word.
x = 0 while x < 5: print(x)
Zero Passes and Infinite Loops
Two boundary cases show up again and again. First, a loop can run its body zero times. If a REPEAT UNTIL condition is already true when the loop is first reached, the body never runs at all. The same happens in Python when a while condition is false from the start.
x = 10 while x < 5: print("run") x = x + 1 print("done")
x <- 10 REPEAT UNTIL (x >= 5) { DISPLAY("run") x <- x + 1 } DISPLAY("done")
Here x is 10, so the Python condition x < 5 is false immediately and the body is skipped; the program prints only done. The pseudocode REPEAT UNTIL (x >= 5) is already true (10 is at least 5), so it also skips the body. A loop running zero times is not an error, it is a legal and common outcome.
Second, an infinite loop happens when the stopping condition never becomes true. In a counter loop this almost always means you forgot to update the counter, so the condition is stuck. Every indefinite loop must contain something that eventually makes it stop.
range(1, 5) gives 1, 2, 3, 4. It stops before the second number. To include 5, you need range(1, 6). Whenever a loop prints or sums the wrong count of items by exactly one, suspect the range bounds first.
total = 0 for i in range(1, 5): total = total + i print(total)
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| Iteration | Repeating a portion of an algorithm | a loop |
| Definite iteration | Repeats a fixed, known number of times |
REPEAT n TIMES / for i in range(n)
|
| Indefinite iteration | Repeats until a condition is met, count unknown |
REPEAT UNTIL(c) / while
|
| Counter | A variable increased by one each pass to tally events | count = count + 1 |
| Accumulator | A variable holding a running total across passes | total = total + i |
| Infinite loop | A loop whose stopping condition never becomes true | counter never updated |
The Create Task rubric awards a point for an algorithm that includes sequencing, selection, AND iteration working together. A loop is not optional decoration; it is one of the three ingredients the readers must be able to point to. Iteration is also how your program processes a whole collection of input instead of a single value.
A point-earning example
Imagine a program that adds up the numbers 1 through n to produce a running total:
In your written response, identify the loop and explain what it repeats and how it stops:
- "The iteration repeats the body once for each value from 1 up to
n, so it runs a number of times that depends on the input." - "On each pass, the accumulator
sumis updated by adding the currentcount, which builds the running total." - "The counter
countincreases every pass, so the stopping conditioncount > neventually becomes true and the loop ends."
The trap to avoid
Make sure the loop actually stops. If you forget the count <- count + 1 line, the condition never becomes true and you have an infinite loop, which earns no point and freezes the program. Also confirm your range covers the right values, an off-by-one loop that misses the first or last item is a partial-credit killer. See the full Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
REPEAT UNTIL (x >= 5) repeats its body until the condition becomes true. Which Python while header loops for exactly the same set of values of x?s is set to 0. Which segments leave s equal to 10 after the loop finishes?- I. s = 0 ; for i in range(1, 5): s = s + i
- II. s = 0 ; for i in range(5): s = s + i
- III. s = 0 ; for i in range(1, 6): s = s + i
n is set. Use a loop to add the numbers 1 through n and print the total. Target output: 15
n is set. Count how many numbers from 1 through n are even, then print the count. Target output: 5
n is set. Multiply the numbers 1 through n together (this is n factorial) and print the result. Start the accumulator at 1, not 0. Target output: 24
6, but it runs forever because the counter is never updated. Add the missing line so the loop stops. Target output: 6
n is set. Use a while loop to print a countdown from n down to 1, each number on its own line. Target output:54321
n, add the round number to a running total, but only when the round number is odd (only odd rounds score). Print the total. Target output: 9
N is given. Print the sum of all the even numbers from 1 to N inclusive. Only the input is provided. Target output: 30
N is given. Print a left-aligned triangle of stars where line 1 has 1 star, line 2 has 2 stars, and so on up to line N. Target output:**********
Frequently Asked Questions
REPEAT n TIMES or for i in range(n). Indefinite iteration repeats until a condition is met and you do not know the count in advance, like REPEAT UNTIL(condition) or a Python while loop.while loop continues while its condition is true and stops when it becomes false. A REPEAT UNTIL loop continues until its condition becomes true, so it stops when the condition is true. To translate one to the other you negate the condition: REPEAT UNTIL(c) is the same as while not c.REPEAT UNTIL condition is already true when the loop is first reached, or a Python while condition is false from the start, the body never runs. Running zero times is a legal, common outcome, not an error.🔗 Continue studying
The Superpack includes an editable Topic 3.8 slide deck with animated loop traces, a REPEAT UNTIL versus while translation drill, an off-by-one and infinite-loop bug bank, a print-ready counter and accumulator worksheet, and a unit quiz. View what's included →
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]