AP CSP Big Idea 1 Sequencing Selection Iteration

AP CSP Topics › Sequencing, Selection & Iteration

AP CSP Sequencing, Selection & Iteration: Complete Guide (2025‑2026)

Every algorithm is built from three fundamental control structures: sequencing (steps run in order), selection (steps run only if a condition is met), and iteration (steps run repeatedly). These three building blocks are sufficient to write any computable program. AP CSP tests your ability to trace through all three, predict outputs, and identify which structure is being used in a given code segment.

3Control structures that are sufficient to write any program
1Output of REPEAT 0 TIMES loop: the body never executes
2Branches in every IF/ELSE: exactly one runs per execution

The Three Control Structures

Sequencing, Selection, and Iteration Sequencing Steps execute in order x ← 5 y ← x + 3 DISPLAY(y) Always runs top to bottom Selection Runs a block only IF condition met IF score ≥ 90 DISPLAY(“A”) ELSE DISPLAY(“Try again”) One path chosen per execution Iteration Repeats a block multiple times REPEAT 3 TIMES DISPLAY(“hello”) Output: hello / hello / hello Same block, 3 executions

Every algorithm uses some combination of these three. A program with no selection always does the same thing. A program with no iteration processes each input only once. A program with no sequencing has no defined order.

Scenario — Identify the Structure

A program does the following: (1) reads a student’s score, (2) checks if the score is ≥60, (3) if yes, prints ‘Pass’ and adds 1 to a pass counter, (4) if no, prints ‘Fail’ and adds 1 to a fail counter, (5) repeats steps 1–4 for each student in the class.

Which control structures are present, and where?

Answer

All three: Sequencing — steps 1–4 execute in order. Selection — step 2 is an IF/ELSE that executes one of two branches based on the score. Iteration — step 5 repeats the entire block for each student. Real programs combine all three constantly.

Tracing Through Combinations

Tracing Sequencing
Execute each step in order
  • x ← 3 → x is 3
  • y ← x + 2 → y is 5
  • x ← y * x → x is 15
  • DISPLAY(x) → outputs 15
  • Each line uses the current value of variables
Tracing Iteration
Track the loop variable each pass
  • total ← 0
  • REPEAT 3 TIMES:
  • Pass 1: total ← 0 + 5 = 5
  • Pass 2: total ← 5 + 5 = 10
  • Pass 3: total ← 10 + 5 = 15
Scenario — Trace the Output

Consider this AP pseudocode:

count ← 0
REPEAT 4 TIMES
  IF count MOD 2 = 0
    DISPLAY(count)
  count ← count + 1


What does this program display?

Work through each iteration before revealing: what values of count are displayed?

Answer


Trace:
Iteration 1: count = 0. 0 MOD 2 = 0 → DISPLAY(0). count becomes 1.
Iteration 2: count = 1. 1 MOD 2 = 1 → not displayed. count becomes 2.
Iteration 3: count = 2. 2 MOD 2 = 0 → DISPLAY(2). count becomes 3.
Iteration 4: count = 3. 3 MOD 2 = 1 → not displayed. count becomes 4.
Output: 0 and 2. The combination of iteration + selection inside the loop displays only even values of count.

Which Structure Does This Use?

Identifying Selection
Look for these signals
  • IF / ELSE IF / ELSE keywords
  • Conditional test (variable compared to value)
  • Two or more branches, only one executes
  • Result depends on the current state
  • Example: assigning a grade based on score
Identifying Iteration
Look for these signals
  • REPEAT N TIMES keyword
  • REPEAT UNTIL keyword
  • FOR EACH in list keyword
  • A block of code that executes multiple times
  • Loop variable or counter changes each pass
Scenario — Selection vs. Iteration

A student argues that this code uses iteration:

IF score ≥ 90
  grade ← “A”
ELSE IF score ≥ 80
  grade ← “B”
ELSE
  grade ← “C”


‘It goes through the conditions multiple times,’ she says.

Is the student correct? What is the key distinction?

Answer

No — this is selection, not iteration. The key distinction: iteration executes a block of code multiple times (different passes through the same code). Selection executes one of several branches once. This IF/ELSE IF/ELSE evaluates the conditions in sequence, but only one branch executes and each condition is checked at most once per execution. No code runs more than once.

Common Exam Pitfalls

1
Confusing REPEAT UNTIL with REPEAT N TIMES

REPEAT N TIMES always executes exactly N times. REPEAT UNTIL runs until a condition becomes true — it may run 0 times if the condition is already true, or run indefinitely if the condition never becomes true (infinite loop).

2
Forgetting that variables retain their values between iterations

Each iteration of a loop uses the current value of all variables, including ones modified in previous iterations. Trace one iteration at a time, updating variable values as you go.

3
Thinking ELSE is required in an IF statement

IF can appear without ELSE. Without ELSE, if the condition is false, nothing happens and execution continues after the IF block. With ELSE, exactly one of the two branches always executes.

4
Miscounting REPEAT N TIMES iterations

If the loop body displays a value, a REPEAT 4 TIMES loop displays it exactly 4 times. Students sometimes count N+1 or N-1 by confusing the loop count with a counter variable that starts at 0.

Check for Understanding

1. Which control structure causes a block of code to execute only when a specific condition is true?

  • Sequencing — steps execute in defined order.
  • Selection — a block executes only if a condition is met.
  • Iteration — a block executes repeatedly.
  • Abstraction — details are hidden from the caller.
Selection (IF/ELSE) executes a block only when the condition evaluates to true. Without selection, the same steps would always execute regardless of conditions.

2. Consider:

x ← 1
REPEAT 3 TIMES
  x ← x * 2
DISPLAY(x)


What is displayed?

  • 2
  • 4
  • 6
  • 8
Trace: Start x=1. Pass 1: x = 1*2 = 2. Pass 2: x = 2*2 = 4. Pass 3: x = 4*2 = 8. DISPLAY(8). Each iteration doubles the previous value of x.

3. Consider these statements about control structures:
I. Sequencing ensures steps execute in a defined order.
II. Selection always requires both IF and ELSE branches.
III. Iteration allows a block of code to execute multiple times.

Which are correct?

  • I only
  • I and III only
  • II and III only
  • I, II, and III
Statement I is correct. Statement III is correct. Statement II is false — IF can appear without ELSE. If the condition is false and there is no ELSE, the program simply skips the IF block and continues.

4. A program checks whether each number in a list is positive, negative, or zero, and counts how many are in each category. Which control structures are required?

  • Sequencing only — to process the list in order.
  • Selection only — to classify each number.
  • Sequencing and iteration only — to process each number in order.
  • Sequencing, selection, and iteration — to process each number (iteration), classify it (selection), and execute steps in order (sequencing).
All three: iteration to process each element in the list, selection to classify each element as positive/negative/zero, and sequencing to execute each step in order within each iteration.

5. A REPEAT UNTIL loop has the condition: REPEAT UNTIL score ≥ 60. If score starts at 80, how many times does the loop body execute?

  • Once — it checks the condition once and then executes.
  • Zero times — the condition is already true before the first iteration.
  • Until score reaches 60.
  • Infinitely — because the condition is already satisfied.
REPEAT UNTIL checks the condition before the first iteration (in AP pseudocode). If the condition is already true (score = 80 ≥ 60), the loop body never executes. Zero iterations.

6. What is the output of this pseudocode?

total ← 0
FOR EACH num IN [2, 4, 6]
  IF num > 3
    total ← total + num
DISPLAY(total)

  • 12 — all three numbers are added.
  • 10 — only 4 and 6 are added.
  • 6 — only the last number is added.
  • 0 — the condition is never true.
Trace: num=2: 2 > 3 is false, skip. num=4: 4 > 3 is true, total = 0+4 = 4. num=6: 6 > 3 is true, total = 4+6 = 10. DISPLAY(10). Only values greater than 3 (4 and 6) are added to total.

Frequently Asked Questions

How does AP pseudocode write iteration differently from Java or Python?
AP pseudocode uses REPEAT N TIMES (for counted loops), REPEAT UNTIL condition (for condition-controlled loops), and FOR EACH item IN list (for list traversal). There is no traditional for-loop with an index variable in AP pseudocode. Java and Python have while loops and for loops with explicit counters; AP pseudocode abstracts these into the three constructs above.
Can a loop run zero times?
Yes. A REPEAT UNTIL loop where the condition is already true before the first iteration runs zero times. A FOR EACH loop over an empty list runs zero times. A REPEAT N TIMES loop with N=0 runs zero times. The AP exam tests this edge case.
What is the difference between a FOR EACH loop and a REPEAT N TIMES loop?
REPEAT N TIMES executes the body exactly N times. FOR EACH iterates over every element in a list, executing the body once per element. The number of iterations for FOR EACH depends on list length, not a fixed number. FOR EACH also gives you access to each list element inside the loop body.

Code Trace Gauntlet

Predict each output before revealing. The AP exam frequently combines sequencing, selection, and iteration in a single trace question.

Trace 1 — Sequencing: Order Matters
x 5 y x + 3 x 10 DISPLAY(y)

y is assigned before x changes to 10. What displays?

Output

8 y = x+3 = 5+3 = 8 (evaluated when x was 5). Changing x to 10 afterward does not retroactively change y. Sequencing: statements execute in written order.

Trace 2 — Selection Inside Loop
count 0 REPEAT 5 TIMES IF count MOD 2 = 0 DISPLAY(count) count count + 1

Which values are displayed?

Output

0 2 4 count goes 0,1,2,3,4. Even values (MOD 2 = 0): 0, 2, 4 are displayed.

Trace 3 — Nested Structure
total 0 FOR EACH n IN [1,2,3,4,5] IF n > 2 total total + n DISPLAY(total)

Only values > 2 are added. What is total?

Output

12 Values > 2: 3, 4, 5. Sum = 3+4+5 = 12.

SPOT THE BUG — Sequencing Error — Display Before Compute
DISPLAY(result) result 42
Bug Explained

result is used before it is defined. Statements execute in sequence — line 1 before line 2. Fix: assign result before displaying it.

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

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com