AP CSA Unit 2: Selection & Iteration - Complete Study Guide (2025)
AP Computer Science A – Unit 2: Selection and Iteration (2025)
Unit 2 combines selection (making decisions with if statements) and iteration (repeating actions with loops). Together, these skills account for roughly 25–35% of the AP CSA exam and show up in almost every FRQ and MCQ.
📘 2.1 Unit Overview & Big Ideas
In this unit, you’ll learn how programs:
-
Decide what to do using boolean expressions and
ifstatements -
Repeat code using
whileandforloops - Combine decisions and repetition to implement algorithms
Main topics:
- Relational & logical operators (
>,<,==,!=,&&,||,!) - Boolean expressions and truth tables
- if, else-if, else, and nested conditionals
- De Morgan’s Law and simplifying conditions
- while loops, for loops, and nested loops
- Loop patterns: counting, summing, searching
- Off-by-one errors and infinite loops
- Tracing and debugging selection + iteration
🧠 2.2 Boolean Expressions & Relational Operators
A boolean expression is an expression that evaluates to either
true or false. You’ll use these inside if-statements and loops.
Relational Operators
| Operator | Meaning | Example |
|---|---|---|
== |
equal to | x == 5 |
!= |
not equal to | x != 0 |
> |
greater than | score > 90 |
< |
less than | temp < 32 |
>= |
greater than or equal to | age >= 16 |
<= |
less than or equal to | count <= 10 |
Examples
int x = 7; boolean a = (x > 5); // true boolean b = (x == 7); // true boolean c = (x != 7); // false
== compares values of primitives (like int, double).
For Strings, you must use equals(), not ==.
🔗 2.3 Logical Operators, Truth Tables & Precedence
Logical operators combine or modify boolean values:
-
&&(AND) – true only if both conditions are true -
||(OR) – true if at least one condition is true -
!(NOT) – reverses a boolean value
Truth Table
| A | B | A && B | A || B |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
NOT Operator
boolean isRaining = true; boolean stayInside = !isRaining; // false
Operator Precedence (Simplified)
- Relational:
>, <, >=, <=, ==, != - Logical NOT:
! - Logical AND:
&& - Logical OR:
||
Short-Circuit Evaluation
Java uses short-circuiting:
-
A && B→ if A is false, B is never evaluated -
A || B→ if A is true, B is never evaluated
if (x != 0 && 10 / x > 1) {
// safe: 10/x only happens if x != 0
}
🧩 2.4 De Morgan’s Law & Simplifying Conditions
De Morgan’s Law lets you distribute a NOT over an AND/OR expression:
Example
Suppose we want to check that a value x is outside the range 1 to 10:
// x is NOT between 1 and 10 inclusive:
if (x < 1 || x > 10) {
...
}
// Equivalent to:
if (!(x >= 1 && x <= 10)) {
...
}
🔀 2.5 if, else-if, else – Making Decisions
Selection lets your program choose between different paths of execution.
Basic if Statement
if (condition) {
// runs only if condition is true
}
if-else
if (score >= 60) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
if-else-if Chain
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else {
grade = "D or F";
}
Example – Two Separate if Statements
int x = 12;
if (x % 2 == 0)
System.out.println("even");
if (x % 3 == 0)
System.out.println("divisible by 3");
Output:
- even
- divisible by 3
Because these are two separate if statements, both can run.
🏛 2.6 Nested Conditionals & Common Decision Patterns
You can place an if-statement inside another if or else block. This is called a nested conditional.
Nested Example
if (age >= 16) {
if (hasPermit) {
System.out.println("You can drive.");
} else {
System.out.println("Get your permit first.");
}
} else {
System.out.println("Too young to drive.");
}
Range Checking Pattern
if (score < 0 || score > 100) {
System.out.println("Invalid score");
} else if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else {
grade = 'D';
}
Multi-Condition Decisions
if (gpa >= 3.5 && communityServiceHours >= 50) {
eligibleForScholarship = true;
}
🧪 2.7 Selection Practice – Predict the Output
Example 1
int x = 5;
if (x > 10)
System.out.println("big");
else if (x > 3)
System.out.println("medium");
else
System.out.println("small");
Output: medium
Example 2
int a = 4;
int b = 7;
if (a > b || b % 2 == 1)
System.out.println("A");
else if (a % 2 == 0 && b % 2 == 0)
System.out.println("B");
else
System.out.println("C");
Output: A (because b % 2 == 1 is true)
Example 3
boolean hot = true;
boolean sunny = false;
if (!hot && sunny)
System.out.println("Cool day");
else if (hot && !sunny)
System.out.println("Humid");
else
System.out.println("Mixed");
Output: Humid
🔁 2.8 Introduction to Iteration (Loops)
Iteration means repeating a block of code multiple times. In Java, the main loop constructs are:
- while loops – repeat while a condition is true
- for loops – repeat a specific number of times
- nested loops – loops inside loops
🔂 2.9 while Loops – Condition-Controlled Repetition
A while loop repeats as long as its condition evaluates to true.
while (condition) {
// body runs repeatedly
}
Counting Example
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Output: 0 1 2 3 4
Key Points
- The condition is checked before each iteration.
- The loop body may run zero times if the condition starts false.
- You must change something inside the loop so the condition eventually becomes false.
Common Mistake – Infinite Loop
int n = 10;
while (n > 0) {
System.out.println(n);
// n never changes → infinite loop
}
🔁 2.10 for Loops – Counting-Controlled Iteration
A for loop is ideal when you know in advance how many times the loop should run.
for (initialization; condition; update) {
// repeated code
}
Typical Example
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Output: 0 1 2 3 4
For Loop Anatomy
- initialization: runs once before the loop starts
- condition: checked before each iteration
- update: runs at the end of each iteration
Counting Down
for (int k = 10; k > 0; k--) {
System.out.println(k);
}
Skipping Values
for (int i = 0; i < 20; i += 2) {
System.out.println(i); // even numbers
}
A for-loop is just a while-loop rewritten more compactly. AP MCQs often show equivalence between the two.
🏗 2.11 Nested Loops
A nested loop is a loop inside another loop. These are common in algorithms that deal with grids, tables, and repeated comparisons.
Example: 2D Pattern
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 5; c++) {
System.out.print("*");
}
System.out.println();
}
Output:
***** ***** *****
How to Trace Nested Loops
Use this mental model:
🧮 2.12 Common Loop Patterns
Almost every FRQ and MCQ relies on one of these patterns.
1. Accumulator Pattern (Summing Values)
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
2. Counting Occurrences
int count = 0;
for (int i = 0; i < list.length; i++) {
if (list[i] % 2 == 0)
count++;
}
3. Linear Search
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
found = true;
break;
}
}
4. Finding Min/Max
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
🔀 2.13 Combining Selection and Iteration
Most real algorithms mix conditionals and loops. AP exam questions assume you can follow these combinations confidently.
Example: Count How Many Numbers Are Positive
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0)
count++;
}
Example: Find First Number Divisible by 7
int index = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 7 == 0) {
index = i;
break;
}
}
Example: Validate Input Using a Loop
while (age < 0 || age > 120) {
System.out.println("Enter a valid age:");
age = input.nextInt();
}
⚠️ 2.14 Common Loop & Selection Mistakes
- Forgetting to update the loop variable (infinite loops)
- Using
==instead ofequals()for Strings - Using
&&when||was intended (and vice versa) - Misunderstanding inclusive vs. exclusive ranges
- Placing
breakin the wrong place inside nested loops - Off-by-one errors: using
<when<=is needed - Tracing loops incorrectly (especially nested loops)
📝 Unit 2 Quiz – Selection & Iteration (10 Questions)
Question 1
What is the output?
int x = 5;
if (x > 10)
System.out.println("big");
else
System.out.println("small");
- A. big
- B. small
- C. big small
- D. no output
Answer: B
Question 2
What is the value of y after the loop?
int y = 0;
for (int i = 1; i <= 3; i++) {
y += i;
}
- A. 3
- B. 6
- C. 4
- D. 1
Answer: B (1+2+3=6)
Question 3
What does the following code print?
int n = 7;
if (n % 2 == 0)
System.out.println("even");
else if (n % 3 == 0)
System.out.println("divisible by 3");
else
System.out.println("other");
- A. even
- B. divisible by 3
- C. other
- D. no output
Answer: C
Question 4
How many times will System.out.println() run?
for (int i = 2; i < 10; i += 2) {
System.out.println(i);
}
- A. 3 times
- B. 4 times
- C. 5 times
- D. 6 times
Answer: C Printed values: 2,4,6,8 → 4 times. But careful—i += 2 from 2 to <10 includes 2,4,6,8 → 4 times, so answer: B (corrected).
Question 5
What does the loop print?
for (int i = 3; i > 0; i--) {
System.out.print(i + " ");
}
- A. 1 2 3
- B. 3 2 1
- C. 0 1 2 3
- D. infinite loop
Answer: B
Question 6
What is printed?
int a = 4;
int b = 9;
if (a < b && b % 3 == 0)
System.out.println("X");
else
System.out.println("Y");
- A. X
- B. Y
- C. XY
- D. no output
Answer: A
Question 7
What is the final value of x?
int x = 1;
int i = 1;
while (i <= 3) {
x *= i;
i++;
}
- A. 6
- B. 3
- C. 0
- D. 1
Answer: A 1×1×2×3 = 6.
Question 8
Which loop prints the even numbers from 2 to 10?
- A.
for (int i=2; i<10; i++) - B.
for (int i=2; i<=10; i+=2) - C.
for (int i=1; i<=10; i++) - D.
for (int i=10; i>0; i-=3)
Answer: B
Question 9
What value is printed last?
for (int i = 0; i <= 4; i++) {
System.out.println(i);
}
- A. 3
- B. 4
- C. 5
- D. none
Answer: B
Question 10
How many times does the inner loop execute?
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 3; c++) {
System.out.println(r + "," + c);
}
}
- A. 4
- B. 3
- C. 7
- D. 12
Answer: D
Outer runs 4 times, inner runs 3 → total 12 iterations.
🚀 Next Steps: Move on to Unit 3 – Class Creation
Now that you’ve mastered selection and iteration, you’re ready for Unit 3: Class Creation, where you’ll learn how to create your own classes, constructors, and methods—skills essential for both FRQs and real-world Java programming.
Add your internal link here:
Continue to AP CSA Unit 3: Class Creation →