AP Computer Science A Unit 2, Selection and Iteration, covers iteration, selection, and decision-making logic across 12 topics, making up 25-35% of the AP exam, with boolean expressions as the central mechanism tying it all together. In AP CSA, that means writing `if` statements, nested `if` statements, `while` loops, and `for` loops in Java. You'll also work with boolean expressions, compound conditions, and string algorithms, plus get an intro to run-time analysis.
AP Computer Science A Unit 2 is where your programs stop running in a straight line and start making choices and repeating work. The single biggest idea is control flow, which means using Boolean expressions to drive if statements (selection) and while and for loops (iteration) so a program can respond to conditions and process data many times. This content makes up 25-35% of the AP exam, the heaviest weight in the course, because nearly every line of AP Java you write from here on lives inside a condition or a loop.
true or false. Relational operators (==, !=, <, >, <=, >=) compare values and produce a Boolean result.== compares actual values. With objects, == compares references (whether two variables point to the same object in memory). That is why strings and other objects use .equals() instead, since classes define their own criteria for what counts as equivalent.!a flips a value, a && b is true only when both are true, and a || b is true when at least one is true. Precedence runs ! first, then &&, then ||.!(a && b) equals !a || !b, and !(a || b) equals !a && !b. Notice how the operator flips when the ! distributes.null using == or != to check whether a variable actually points to an object before you call methods on it.if statement runs a block of code only when its Boolean expression is true. One-way selection skips the block entirely when the condition is false.if-else) guarantees exactly one of two paths runs. Multiway selection (if-else-if) checks a series of conditions in order and runs at most one block, the first one whose condition is true.if statements put a decision inside another decision. The inner condition is only evaluated when the outer condition is true, so structure and ordering change behavior. Separate if statements can all fire; an if-else-if chain fires at most once.while loop checks its condition before every iteration, including the first. If the condition starts false, the body never runs at all. If the condition never becomes false, you get an infinite loop.for loop packs three parts into its header. The initialization runs once and creates the loop control variable, the Boolean expression is checked before each pass, and the update runs after each pass. It is a while loop with the bookkeeping built in.< and <= or starting the counter at the wrong value. Trace the first and last iterations to catch them.n % d == 0 means d divides n evenly, which is how you test for even, odd, or multiples.n % 10 (grab the last digit) and n / 10 (drop the last digit), looped until n reaches 0.charAt or substring to check whether substrings have a property, count substrings that meet a criterion, or build a reversed copy one character at a time.n times and the inner loop runs m times per outer pass, the inner body executes n * m times.| Topic | Core idea | Watch out for |
|---|---|---|
| Algorithms with selection and repetition | Sequencing, selection, and repetition are the three building blocks of every algorithm | The order you combine them changes the outcome |
| Boolean expressions | Relational operators produce true or false | == on objects compares references, not contents |
if / if-else / if-else-if | One-way, two-way, and multiway branching | Only the first true branch of a chain runs |
Nested if statements | Decisions inside decisions; inner runs only if outer is true | Misreading which else pairs with which if |
| Compound Booleans and De Morgan's law | !, &&, ` | |
while loops | Condition checked before every iteration | Body may run zero times; forgetting the update causes infinite loops |
for loops | Initialization, condition, update packed in the header | Update runs after each pass, before the next condition check |
| Nested iteration | Inner loop completes fully each time the outer loop runs once | Total inner-body executions multiply |
| Selection and iteration algorithms | Divisibility, digit extraction, sum, average, min, max, frequency | Initialize accumulators before the loop, not inside it |
| String algorithms | Loop over characters to test, count, or reverse | substring(i, i + 1) grabs one character as a String |
| Informal run-time analysis | Count how many times a statement executes | Off-by-one in your trace skews the whole count |
This unit carries the course's central skill set, which is developing code to implement an algorithm and determining what code produces when it runs. Selection and iteration are not one chapter you finish; they are the grammar of everything that follows.
substring and charAt from Using Objects and Methods (Unit 1). The reference vs. primitive distinction from Unit 1 explains why == behaves differently on objects.equals method, for example, is a Boolean expression comparing attributes.if inside; selection sort and insertion sort are nested iteration; traversing a 2D array is the row-by-column nested loop pattern from Topic 2.11.if (condition) { } else if (condition) { } else { } branches execution; at most one block in the chain runs.while (condition) { } repeats as long as the condition is true, checking before each pass (zero or more executions).for (int i = 0; i < n; i++) { } runs the body exactly n times; initialization once, condition before each pass, update after each pass.a && b, a || b, !a combine Booleans; precedence is !, then &&, then ||.!(a && b) is !a || !b and !(a || b) is !a && !b.obj1.equals(obj2) compares object contents; obj1 == obj2 compares references; ref == null checks for a missing object.n % d == 0 is true when d evenly divides n.digit = n % 10; n = n / 10; until n == 0 to process each digit.sum = 0, count = 0, or max = first value before the loop, then update inside it; compute average as (double) sum / count to avoid integer division.rev = str.substring(i, i + 1) + rev; (or loop backward and append) across all indexes.i = 1 to i <= n runs n times; nested loops multiply their counts.This unit is worth 25-35% of the exam, more than any other. On the multiple-choice section, expect to determine the result of code segments. You'll trace loops to find what prints, count how many times a statement executes, identify which Boolean expression is equivalent to a given one (De Morgan's law shows up constantly), and spot off-by-one and infinite-loop bugs. Some questions hand you several code segments and ask which ones produce identical output.
On the free-response section, selection and iteration are the muscle behind nearly every rubric point. Methods FRQs ask you to write loops with conditions inside them, including accumulator patterns like counting matches, summing values, or finding a max, and string-processing FRQs require you to traverse a string with substring or charAt and apply criteria to each piece. Even when an FRQ is officially about classes or arrays, the points you earn usually come from a correctly bounded loop and a correctly written condition.
while loop and a for loop, and how do you guarantee a loop runs the right number of times?true or false, built from relational and logical operators.==, !=, <, >, <=, >=, which compare values and produce a Boolean.!, &&, and ||, which negate and combine Boolean expressions.! across && and ||, flipping the operator in the process.if-else-if chain where at most one branch executes, the first true one.for loop header.< vs. <= slip.== vs. .equals() on objects. == asks "same object in memory?" while .equals() asks "equivalent according to the class?" Two different String objects with identical characters are .equals() but may not be ==.if statements vs. one if-else-if chain. Separate ifs can all execute on the same pass; a chain executes at most one branch. Swapping one for the other changes the output.while loop condition placement. The condition is checked before every iteration, so a loop can legitimately run zero times. It is not "run once, then check."! without flipping the operator. !(a && b) is not !a && !b. De Morgan's law requires && to become || (and vice versa) when the negation moves inside.