Now readingIntroduction
0%
AP Computer Science A · Framework 2026-27
Unit 2

Selection and Iteration

Conditions choose paths and loops revisit paths. Together they express searches, counts, simulations, string processing, and most other algorithms in the course.

Exam weight
25–35%
Topics
12

Introduction

Conditions choose paths and loops revisit paths. Together they express searches, counts, simulations, string processing, and most other algorithms in the course.

Correct tracing depends on execution order. Evaluate a condition only when control reaches it, record every state change, and identify the invariant that remains true after each complete iteration. Then prove termination by naming the value that moves toward an exit.

Current exam guidance: Aligned to the current 2026–27 AP Computer Science A framework and Java Quick Reference. The May 2027 digital exam has 42 multiple-choice questions in 90 minutes and four free-response questions in 90 minutes.
2.1

Algorithms with Selection and Repetition

How do decisions and repeated steps turn a straight-line procedure into an algorithm?

Short answer

Selection chooses which steps run from current conditions, while iteration repeats steps until a bound or stopping condition is reached.

Know firstAssignments · Boolean values

Control flow determines which statement comes next

A sequential algorithm runs statements once in order. Selection adds branches: a condition is evaluated, then exactly the matching branch runs. Iteration adds a cycle: after the loop body, execution returns to a condition or update before deciding whether another pass occurs.

Trace control flow and data state together. A loop is correct only when its initialization establishes a usable starting state, its condition admits exactly the intended cases, its body makes progress, and its exit state satisfies the goal. A condition can be syntactically valid yet select the wrong cases.

Java trace

Combine a counted loop with a selection

int total = 0;
for (int value = 1; value <= 6; value++) {
  if (value % 2 == 0) {
    total += value;
  }
}
System.out.println(total);
Execution stateStep 1 of 6
value
1
even
false
totalAfter
0
Open complete trace table
Complete trace states
StepvalueeventotalAfter
11false0
22true2
33false2
44true6
55false6
66true12
Output
12

The loop visits one through six. Only two, four, and six satisfy the even condition, so the accumulator ends at twelve.

Sketch the path before tracing values

  1. Mark the initialization.
  2. At each decision, record the Boolean result.
  3. Execute only the selected statements.
  4. After a loop body, apply the update and retest the condition.
  5. Check the first state after exit.

Keep these three ideas

  • Selection chooses a path.
  • Iteration revisits a controlled block.
  • Correct loops establish state, make progress, and exit.

Keep reading Unit 2: Selection and Iteration

You just read the first topic free. Your free trial opens the rest of this unit, every AP Computer Science A unit, and all 19 AP courses.

  • 2.1 Algorithms with Selection and RepetitionRead
  • 2.2 Boolean Expressions
  • 2.3 if Statements
  • 2.4 Nested if Statements
  • 2.5 Compound Boolean Expressions
  • 2.6 Comparing Boolean Expressions
  • 2.7 while Loops
  • 2.8 for Loops
  • 2.9 Implementing Selection and Iteration Algorithms
  • 2.10 Implementing String Algorithms
  • 2.11 Nested Iteration
  • 2.12 Informal Run-Time Analysis
Start my free 7 days →$0.00 due today · cancel anytime · score guarantee

Practice AP Computer Science A in the real test engine.

Free practice set: timed, the same engine as our full-length tests, every answer explained.