Home ›
AP Computer Science A ›
Unit 2 ›
Visual Review
AP Computer Science A Unit 2 Visual Review
A topic-by-topic visual walkthrough of Selection and Iteration — Boolean expressions, if statements, while and for loops, nested iteration, and run-time analysis.
← Back to Unit 2 hub
TOPIC 2.1
Algorithms with Selection & Repetition
Three ways control flows through a program
Every algorithm is built from three control structures. Combining them lets you express any computation.
This unit adds selection and repetition on top of the sequencing from Unit 1.
Sequence
Statements run in order,
top to bottom.
a = 1;
b = a + 2;
Selection
Choose a path based on
a condition.
if (x > 0) { ... }
else { ... }
Repetition
Repeat steps with a loop
while a condition holds.
while (i < n) { ... }
for (...) { ... }
All algorithms combine sequence, selection, and repetition .
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.2
Boolean Expressions
Relational operators
< <= > >=
== (equal) != (not equal)
Each yields a boolean: true or false.
5 > 3 → true
4 == 5 → false
== vs = — a common bug
==
tests equality (a comparison)
=
assigns a value (changes it)
For objects & Strings, use .equals()
to compare contents, not ==.
== on objects compares references only.
The NOT operator
! flips a boolean: !true → false, !(x > 5) is true exactly when x is 5 or less.
Relational operators return a boolean; == compares, = assigns .
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.3
if Statements
if (score >= 60) {
print("Pass" );
} else if (score >= 50) {
print("Retake" );
} else {
print("Fail" ); }
How it flows
The if condition is checked first.
If true → run that block, SKIP the rest.
else if is only checked when earlier
conditions were false.
else runs when none matched.
Only ONE branch of an if / else-if / else chain runs
Order matters: put the most specific / restrictive condition first, or a broader one may catch it early.
An if / else-if / else chain runs exactly one branch — the first true one.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.4
Nested if Statements
if (loggedIn) {
if (isAdmin) {
showAdminPanel();
} else {
showUserPage();
} }
An if inside another if
The inner if is only reached when the
outer condition is true.
Use it for decisions that depend on a
prior decision.
An else pairs with the NEAREST unmatched if.
Often replaceable with &&
if (loggedIn) { if (isAdmin) {...} } is equivalent to if (loggedIn && isAdmin) {...} when there's no else.
A nested if runs only when the outer condition is already true.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.5
Compound Boolean Expressions
TRUTH TABLE
A B A&&B A||B
T T T T
T F F T
F T F T
F F F F
&& (AND) · || (OR)
&& is true only if BOTH sides are true.
|| is true if AT LEAST ONE side is true.
SHORT-CIRCUIT: if the left of && is
false (or left of || is true), the right
side is never evaluated.
This can safely guard against errors.
&& needs both true, || needs one — and both short-circuit.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.6
Comparing Boolean Expressions
De Morgan's Laws — how to negate compound conditions
!(A && B) ≡ !A || !B
!(A || B) ≡ !A && !B
Equivalent expressions
Two boolean expressions are equivalent
if they give the same result for EVERY
combination of inputs.
Check by building a truth table for both.
Example
"NOT (age < 18 or noID)" becomes:
age >= 18 && !noID
Flip each comparison and swap && ↔ ||.
Note: !(a < b) becomes a >= b.
De Morgan's: negating flips each term and swaps && ↔ ||.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.7
while Loops
// print 1 through 5
int i = 1 ;
while (i <= 5) {
print(i);
i++; // update!
}
Three parts of every loop
1. INITIALIZE the loop variable.
2. TEST the condition each pass.
3. UPDATE it toward the exit.
Forget the update → INFINITE LOOP.
The condition is checked BEFORE each pass.
Use while when the number of iterations isn't known up front
e.g. "keep reading input until the user types 'quit'." If the condition is false at the start, the body never runs.
A while loop tests before each pass — always move toward the exit condition.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.8
for Loops
for (int i = 0 ; i < n ; i++ ) { ... }
initialize
test (before each pass)
update (after each pass)
Same three parts, one line
A for loop bundles the init, test, and
update into its header.
Best when you know how many times
to loop (a counted loop).
Off-by-one care
i < n runs n times (i = 0..n−1).
i <= n runs n+1 times.
A for and while can express the
same loop — they're interchangeable.
A for loop packs init; test; update into one header — ideal for counted loops.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.9
Implementing Selection & Iteration Algorithms
// count multiples of 3 in 1..n
int count = 0 ;
for (int i = 1; i <= n; i++) {
if (i % 3 == 0)
count++;
}
Common loop patterns
COUNTER: tally items meeting a test.
ACCUMULATOR: build a running sum
or product.
MIN / MAX: track the best value seen.
FLAG: a boolean that records a fact.
Put selection INSIDE iteration
Nesting an if inside a loop lets you act only on items that meet a condition — the core of most algorithms.
Combine loops with ifs using counter, accumulator, and min/max patterns.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.10
Implementing String Algorithms
// count the vowels in a String s
for (int i = 0; i < s.length(); i++) {
String ch = s.substring(i, i + 1);
if ("aeiou" .indexOf(ch) >= 0) count++;
}
Traverse by index
Loop i from 0 to length()−1 and pull
out one character with substring(i,i+1).
Watch the bounds
Going past length()−1 throws an
IndexOutOfBoundsException.
Traverse a String with i from 0 to length()−1 and substring(i, i+1).
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.11
Nested Iteration
// prints a 3 x 4 grid of stars
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 4; c++)
print("*" );
println();
}
Inner loop finishes each pass
For every ONE pass of the outer loop,
the inner loop runs completely.
Total iterations = outer × inner.
Here: 3 rows × 4 columns = 12 stars.
Common for grids & 2D data (Unit 4).
Trace carefully
On the exam, track the outer and inner counters side by side to predict the exact output.
Nested loops run inner fully for each outer pass — total = outer × inner.
The Review Hub · AP Computer Science A Unit 2
TOPIC 2.12
Informal Run-Time Analysis
Count how many times the key statement runs
Run-time is estimated by counting the total loop iterations (statement executions) as the input n grows.
No formal Big-O notation is required — just compare growth informally.
Single loop
Runs about n times.
Double n → double
the work.
"linear" growth
Nested loop
Runs about n × n = n²
times.
Double n → 4× the work.
"quadratic" growth
Why it matters
n² grows much faster
than n for large n.
Fewer nested loops =
faster programs.
⏱Count iterations: one loop ≈ n, nested loops ≈ n² .
The Review Hub · AP Computer Science A Unit 2
▤ Show all slides
How to use the visual review
Spend 30 seconds per slide before clicking next. Look at the code, then ask yourself: "Could I trace this loop or predict this output from memory?"
Use the fullscreen button () on desktop for the best experience. Use arrow keys to navigate. Tap "Show all slides" to jump around.
This is great for review the night before the exam — fast, visual, and covers every idea you need to recognize in Unit 2.