AP CSP Topic 3.8 Guided Notes - Iteration

Big Idea 3: Algorithms and Programming · Topic 3.8 · Guided Notes (Student)

Iteration — Guided Notes

Fill these in during class or catch up here if you were absent. Print this page or work on paper — then check yourself with the CFUs on the Topic 3.8 page.

Print these notesAll CSP topics

Day 1: Doing It Again (and Again)

Today’s objectives

  • Express a repeating process as iteration without using a programming language, saying whether it repeats a set number of times or until a condition is met (LO AAP-2.J)
  • Write iteration statements using REPEAT n TIMES and REPEAT UNTIL(condition) from the exam reference sheet (LO AAP-2.K)

Bell ringer

Maya is saving for a $20 game. She writes her plan out longhand: 'Add $5. Add $5. Add $5. Add $5.' Then she learns next month's game will cost $35 — and the month after that, who knows.

Write 2–4 sentences: Rewrite Maya's plan so it works for ANY price without listing 'Add $5' over and over. You will likely find you need TWO kinds of instruction — one that repeats a set number of times, and one that repeats until she reaches the price. Write both.

01. Iteration Without a Language

Key Vocabulary (LO AAP-2.J, AAP-2.K)

Term Definition (write it)
Iteration
Loop
REPEAT n TIMES
REPEAT UNTIL
Stopping condition

What Iteration Is

  • Iteration means a portion of an algorithm .
  • The two ways a repeat can be controlled are a set count or .
  • Iteration is the third algorithm building block, joining .

Say It Once, Not Ten Times

Unrolled by hand

Expressed as iteration

  • The advantage of expressing a repeat as iteration instead of writing every line is that .
  • When you describe iteration without code, first decide whether the repeat is controlled by .

AP TIP: AAP-2.J asks you to express iteration WITHOUT code — decide first whether the repeat is controlled by a count or by a condition, then say it in one sentence.

Stop and think

  1. In two sentences, explain what makes a process 'iteration.' Use the phrase 'repeating portion of an algorithm.'
  2. For each, say whether it is count-controlled or condition-controlled, and why: (a) do 20 jumping jacks, (b) keep stirring until the sauce thickens, (c) knock on the door until someone answers.
  3. Describe, in one sentence and no code, an everyday repeating task of your own — then state which of the two controls it uses.

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.8 page.

02. Writing the Two Loop Forms

Two Loops on the Reference Sheet

  • An iteration statement changes the flow of control by .
  • Choose REPEAT n TIMES when .
  • Choose REPEAT UNTIL when .

A Fixed Count

savings starts at 0; the block runs exactly 4 times, adding 5 each pass, ending at 20.

AP Pseudocode (what the exam tests)

savings ← 0
REPEAT 4 TIMES
{
  savings ← savings + 5
}
DISPLAY(savings)

Python (the runnable version)

savings = 0
for i in range(4):
    savings = savings + 5
print(savings)

Output

20

Run and edit this yourself in the coding exercises for this topic.

Until a Condition Is Met

The same +5 block, but now it repeats UNTIL savings ≥ 20 — the condition, not a count, ends it.

AP Pseudocode (what the exam tests)

savings ← 0
REPEAT UNTIL(savings ≥ 20)
{
  savings ← savings + 5
}
DISPLAY(savings)

Python (the runnable version)

savings = 0
while savings < 20:
    savings = savings + 5
print(savings)

Output

20

Run and edit this yourself in the coding exercises for this topic.

Build a REPEAT Loop, Pass by Pass

This is the REPEAT 4 TIMES loop that adds 5 to savings. Fill in savings AFTER each pass runs. 'Start' is before any pass.

Complete the empty cells.

After pass # Statement run savings
Start savings ← 0 0
1 savings ← savings + 5 5
2 savings ← savings + 5
3 savings ← savings + 5
4 savings ← savings + 5

Watch out — “REPEAT UNTIL Runs a Number of Times You Set”

Myth: A REPEAT UNTIL loop runs a fixed number of times that the programmer chooses, just like REPEAT n TIMES.

Explain why this is wrong:

Stop and think

  1. Write a REPEAT n TIMES loop in exam pseudocode that displays the word GO five times. Then say how many times the block runs and how you know.
  2. Rewrite this as a REPEAT UNTIL loop: 'starting from count ← 0, add 1 to count until count reaches 6.' Then state the stopping condition you used.
  3. A task says 'ring the bell 3 times.' Which loop form fits, and why would REPEAT UNTIL be the wrong choice here?

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.8 page.

Today in one box

  • Iteration is a repeating portion of an algorithm; it repeats a set number of times or until a condition is met
  • You can express iteration without code — decide whether a repeat is count-controlled or condition-controlled
  • REPEAT n TIMES { } runs its block exactly n times — a fixed count
  • REPEAT UNTIL(condition) { } repeats until a Boolean condition becomes true — the condition, not a number, ends it

Before next class: Predict it for tomorrow: savings ← 25, then REPEAT UNTIL(savings ≥ 20) { savings ← savings + 5 }, then DISPLAY(savings). How many times does the block run, and what prints? Write your answer AND your reasoning.

Day 2: Reading a Loop's Result

Today’s objectives

  • Determine the result or side effect of REPEAT n TIMES and REPEAT UNTIL loops by tracing them pass by pass (LO AAP-2.K)
  • Explain the two edge cases of REPEAT UNTIL: an infinite loop when the condition never becomes true, and zero executions when the condition is true before the loop starts (LO AAP-2.K)

Bell ringer

Yesterday's teaser, now settle it. The program runs: savings ← 25, then REPEAT UNTIL(savings ≥ 20) { savings ← savings + 5 }, then DISPLAY(savings). Three classmates predict it prints 30, 25, and 'it never stops.'

Who is right, and exactly why? Write the value the loop checks BEFORE the first pass, decide how many times the body runs, and name the mistake each wrong classmate made.

01. Tracing a Loop's Result

How to Trace Any Loop

  • To find what a loop produces, build a table with one row per .
  • For a REPEAT UNTIL loop you must test the condition .
  • A loop's result or side effect is .

Count the Passes

This REPEAT UNTIL adds 5 to savings and counts weeks; the counter reveals how many passes the condition allowed.

AP Pseudocode (what the exam tests)

savings ← 0
weeks ← 0
REPEAT UNTIL(savings ≥ 20)
{
  savings ← savings + 5
  weeks ← weeks + 1
}
DISPLAY(weeks)

Python (the runnable version)

savings = 0
weeks = 0
while savings < 20:
    savings = savings + 5
    weeks = weeks + 1
print(weeks)

Output

4

Run and edit this yourself in the coding exercises for this topic.

Trace a REPEAT UNTIL, Pass by Pass

The loop is REPEAT UNTIL(savings ≥ 20) { savings ← savings + 5; weeks ← weeks + 1 }. The condition is checked BEFORE each pass. Fill in the blanks.

Complete the empty cells.

Check # (before body) savings ≥ 20? savings after body weeks after body
1 0 ≥ 20 → false, run body 5 1
2 5 ≥ 20 → false, run body 2
3 10 ≥ 20 → false, run body 15 3
4 15 ≥ 20 → false, run body 20
5

Stop and think

  1. Trace: total ← 0; REPEAT 5 TIMES { total ← total + 2 }; DISPLAY(total). Show total after each pass and give the final value.
  2. Trace: x ← 1; count ← 0; REPEAT UNTIL(x > 8) { x ← x + 3; count ← count + 1 }; DISPLAY(count). How many passes run, and what prints?
  3. In one sentence, explain why a counter variable inside a REPEAT UNTIL is a reliable way to answer 'how many times did the loop run?'

Show every step. Then check yourself with the matching CFUs on the Topic 3.8 page.

02. Two Subtle Cases

Case 1 — The Infinite Loop

  • A REPEAT UNTIL becomes an infinite loop when .
  • total ← 1 with +2 each pass never satisfies total = 10 because .
  • The way to avoid an infinite loop is to ensure every pass .

The Loop That Never Stops

savings never grows, so savings ≥ 20 can never become true — this loop runs forever (do not run it as-is).

AP Pseudocode (what the exam tests)

savings ← 0
REPEAT UNTIL(savings ≥ 20)
{
  savings ← savings + 0
}
DISPLAY(savings)

Python (the runnable version)

savings = 0
while savings < 20:
    savings = savings + 0
# never reaches print(savings)

Run and edit this yourself in the coding exercises for this topic.

Case 2 — The Zero-Times Loop

  • A REPEAT UNTIL body runs zero times when .
  • The reason a zero-times run is possible at all is that the condition is checked .
  • After a zero-times loop, the tracked variables hold .

The Loop That Never Starts

savings starts at 25, so savings ≥ 20 is already true; the body runs zero times and 25 prints unchanged.

AP Pseudocode (what the exam tests)

savings ← 25
REPEAT UNTIL(savings ≥ 20)
{
  savings ← savings + 5
}
DISPLAY(savings)

Python (the runnable version)

savings = 25
while savings < 20:
    savings = savings + 5
print(savings)

Output

25

Run and edit this yourself in the coding exercises for this topic.

Watch out — “A REPEAT UNTIL Loop Always Runs at Least Once”

Myth: The body of a REPEAT UNTIL loop must execute at least one time before the condition is ever checked.

Explain why this is wrong:

Stop and think

  1. For k ← 3; REPEAT UNTIL(k = 0) { k ← k − 1 }: how many passes run, and does it stop? Now change the body to k ← k − 2 and answer again.
  2. For n ← 8; REPEAT UNTIL(n ≥ 5) { n ← n + 1 }; DISPLAY(n): how many times does the body run, and what prints? Name the case.
  3. Write a one-sentence checklist you can use on the exam to decide whether a REPEAT UNTIL will loop forever, run zero times, or run a normal number of passes.

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.8 page.

Common AP Traps

Three ways Topic 3.8 loses points on the exam — one minute now, real points in May.

REPEAT UNTIL has no built-in count — in your own words:

The condition is checked BEFORE the body — in your own words:

Watch for the infinite loop — in your own words:

Iteration, in One Slide

  • Iteration is a repeating portion of an algorithm — a set number of times, or until a condition is met.
  • REPEAT n TIMES runs its block exactly n times; REPEAT UNTIL(condition) repeats until the condition becomes true.
  • Determine a loop's result by tracing it pass by pass — one row per pass, checking a REPEAT UNTIL condition before each pass.
  • REPEAT UNTIL edge cases: it loops forever if the condition never becomes true (K.4), and runs zero times if the condition is true before it starts (K.5).

Exit check — I can…

  • ☐  express a repeating process as iteration without code, saying whether it repeats a set count or until a condition (LO AAP-2.J)
  • ☐  write REPEAT n TIMES and REPEAT UNTIL(condition) statements using the exam reference sheet (LO AAP-2.K)
  • ☐  trace a loop pass by pass to determine its result or side effect (LO AAP-2.K)
  • ☐  identify when a REPEAT UNTIL loop runs zero times or becomes an infinite loop (LO AAP-2.K)

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]