SAT / PSAT
SAT / PSAT Prep
History & Social Science
AP World History AP US History AP European History AP Human Geography AP US Government & Politics AP Psychology AP Macroeconomics AP Microeconomics
English
AP English Language & Composition AP English Literature & Composition
Math & Computer Science
AP Calculus AB/BC AP Precalculus AP Statistics AP Computer Science A AP Computer Science Principles
Sciences
AP Biology AP Chemistry AP Environmental Science AP Physics 1 AP Physics 2
World Languages & Arts
AP Spanish Language AP Art History AP Music Theory Start studying →
Unit 2 · Selection & Iteration

Selection and Iteration

Give your programs the power to decide and repeat. Write Boolean expressions, branch with if/else and nested conditions, combine tests with &&, ||, and !, and loop with while and for — then apply them to string and standard algorithms.

12 topics
25–35% of the exam
~29–31 class periods
College Board aligned
← Back to AP Computer Science A

Choose your study tool

Six ways to master Unit 2 — pick whichever fits how you like to study.

Flashcards
24 interactive flashcards covering Boolean logic, if/else, while and for loops, and nested iteration. Tap to flip.
Open flashcards →
Cheat Sheet
A one-page visual summary of Unit 2 — every operator, statement, and loop pattern on a single screen.
Open cheat sheet →
Essentials
The core concepts plus a searchable glossary of every vocabulary term you need to know for the exam.
Open essentials →
Visual Review
A slide-by-slide walkthrough of Unit 2 with truth tables, control-flow diagrams, and loop tracing.
Open visual review →
MCQ Practice
35 multiple-choice questions in College Board exam style — with full explanations of every answer.
Start practice →
FRQ Practice
A free-response question with model answers showing exactly how each part earns its point on the exam.
Start FRQ →

Topics in Unit 2

All 12 topics from the College Board CED, in order.

Topic 2.1
Algorithms with Selection & Repetition
Building algorithms from sequencing, selection, and iteration.
Topic 2.2
Boolean Expressions
Relational operators and expressions that evaluate to true or false.
Topic 2.3
if Statements
One-way and two-way selection with if and if/else.
Topic 2.4
Nested if Statements
if statements inside other if statements, and else-if chains.
Topic 2.5
Compound Boolean Expressions
Combining conditions with the logical operators &&, ||, and !.
Topic 2.6
Comparing Boolean Expressions
Equivalent conditions, truth tables, and De Morgan’s laws.
Topic 2.7
while Loops
Repeating code while a condition remains true.
Topic 2.8
for Loops
Counting loops with initialization, condition, and update.
Topic 2.9
Implementing Selection & Iteration
Standard algorithms combining conditionals and loops.
Topic 2.10
Implementing String Algorithms
Traversing and processing Strings with loops.
Topic 2.11
Nested Iteration
Loops inside loops for grids and combinations.
Topic 2.12
Informal Run-Time Analysis
Counting how many times statements execute.

About Unit 2

Algorithms are built from three pieces: sequencing (Unit 1), selection, and iteration — and Unit 2 is all about the last two. Selection lets a program make decisions: you write Boolean expressions with relational operators (<, >, ==, !=), branch with if and if/else, nest conditions, and combine them with the logical operators && (and), || (or), and ! (not). You’ll compare equivalent conditions using truth tables and De Morgan’s laws.

Iteration repeats code. A while loop runs as long as its condition is true; a for loop packages initialization, condition, and update for counting. You’ll apply these to standard algorithms and String traversals, write nested loops for grids and combinations, and do informal run-time analysis by counting how many times statements execute. Watch for infinite loops and off-by-one errors — the most common bugs in this unit.

On the exam this unit is 25–35% of the multiple-choice section — one of the largest — and takes about 29–31 class periods. The five computational thinking practices below run through every topic:

Practice 1
Design Code
Practice 2
Develop Code
Practice 3
Analyze Code
Practice 4
Document Code
Practice 5
Use Computers Responsibly
Up next
Unit 3: Class Creation
Start Unit 3 →