AP CSP Day 56: Creative Commons

Big Idea 3: Algorithms & Programming
Cycle 2 • Day 56 Practice • Hard Difficulty
Focus: Iteration Patterns

Practice Question

What is displayed after the following code runs?

x ← 1
total ← 0
REPEAT UNTIL x > 100
{
   total ← total + x
   x ← x * 2
}
DISPLAY(total)
Why This Answer?

Trace: x=1,total=0. Iteration 1: total=1,x=2. Iteration 2: total=3,x=4. Iteration 3: total=7,x=8. Iteration 4: total=15,x=16. Iteration 5: total=31,x=32. Iteration 6: total=63,x=64. Iteration 7: total=127,x=128. Now x=128>100, loop stops. total=127.

Why Not the Others?

A) 63 is total after 6 iterations, but x=64 is not > 100, so one more iteration occurs. C) 100 is the threshold, not the sum. B) 255 would require one additional iteration (x=256), but x=128 already exceeds 100.

Common Mistake
Watch Out!

Students stop one iteration too early (at total=63 when x=64) because 64 seems close to 100. The condition x > 100 is not met until x=128, so the iteration where x goes from 64 to 128 does execute.

AP Exam Tip

REPEAT UNTIL checks the condition BEFORE each iteration. If the condition is still false (x ≤ 100), the loop body executes. Continue iterating until the condition is checked and found to be true.

Keep Practicing!

Consistent daily practice is the key to AP CSP success.

AP CSP Resources Get 1-on-1 Help
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.