AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/2)
Unit 2
25–35% of examSelection and Iteration
Unit 2 teaches your programs to make decisions and repeat work. You write Boolean expressions, use if statements to choose what runs, and use while and for loops to repeat steps, then combine them into standard algorithms with numbers and strings. It's the heart of the first free-response question and a big share of the multiple-choice section.
Study this unit
Flashcards (35)Practice questions (61)Computer Science A must-know sheetFree-response questions on this unit
Write your own answer, then score it with the rubric or with AI.
- Question 1: Methods and Control StructuresTrain route delays and station codes7 points · about 25 minutes
- Question 1: Methods and Control StructuresDigit-sum puzzle and mirror strings7 points · about 25 minutes
- Question 1: Methods and Control StructuresRewards card constructor and top item7 points · about 25 minutes
- Question 1: Methods and Control StructuresNetwork outages and hidden email addresses7 points · about 25 minutes
- Question 1: Methods and Control StructuresAverage with the lowest score dropped, and name formatting7 points · about 25 minutes
- Question 2: Class DesignParking meter7 points · about 25 minutes
- Question 2: Class DesignThermostat modes7 points · about 25 minutes
- Question 2: Class DesignOverlapping time slots7 points · about 25 minutes
- Question 4: 2D ArrayCounting peaks on a height map6 points · about 22 minutes
Big ideas
- Every algorithm is built from three moves: doing steps in order, choosing between paths, and repeating
- Boolean expressions decide which code runs and how long a loop keeps going
- De Morgan's laws turn a negated condition into an equivalent one
- Most loop bugs are off-by-one errors or conditions that never become false
- Tracing code line by line is the surest way to know what it does
Full unit reviews
Longer videos that cover the whole unit. Good for a first pass or a final review.
Topics
- 2.1: Algorithms with Selection and Repetition
- 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
Algorithms are built from three ideas: sequencing (steps in order), selection (choosing a path based on a true-or-false question) and repetition (repeating steps until a goal is reached). Changing the order of these pieces can change the result, which you can see by writing the steps out or drawing a flowchart.
Key terms
- sequencing
- selection
- repetition
- flowchart
A few quick questions on this topic, with the answers explained.
Relational operators (==, !=, <, >, <=, >=) compare two values and produce a boolean. With primitives, == compares the actual values; with objects, it checks whether two references point to the same object.
Key terms
- Boolean expression
- relational operator
==vs!=boolean
A few quick questions on this topic, with the answers explained.
An if statement runs its body only when its condition is true (one-way selection). Adding an else gives two-way selection: exactly one of the two blocks runs, depending on the condition.
Key terms
ifstatementif-else- one-way selection
- two-way selection
- flow of control
A few quick questions on this topic, with the answers explained.
A nested if sits inside another if, so its condition is checked only when the outer condition is true. An if-else-if chain is multiway selection: Java checks the conditions in order, runs only the first block whose condition is true, and runs the final else (if there is one) when none are.
Key terms
- nested
if if-else-if- multiway selection
- trailing
else
A few quick questions on this topic, with the answers explained.
The logical operators ! (not), && (and) and || (or) combine Boolean expressions, and they're applied in that order: ! first, then &&, then ||. Java uses short-circuit evaluation, so if the left side of && is false or the left side of || is true, the right side is never checked.
Key terms
- logical operator
&&||!- short-circuit evaluation
- truth table
A few quick questions on this topic, with the answers explained.
Two Boolean expressions are equivalent when no combination of values makes them disagree, and a truth table lets you check every case. De Morgan's laws say !(a && b) equals !a || !b and !(a || b) equals !a && !b. With objects, == and != check whether two variables are aliases of the same object (or null), while a class's equals method compares their contents.
Key terms
- equivalent expressions
- De Morgan's laws
- truth table
- alias
equalsmethod
A few quick questions on this topic, with the answers explained.
while Loops
A while loop checks its condition before every pass and keeps running its body as long as the condition is true. If the condition starts out false the body never runs, and if it can never become false you get an infinite loop. Looping one time too many or too few is called an off-by-one error.
Key terms
- iteration
whileloop- loop condition
- infinite loop
- off-by-one error
A few quick questions on this topic, with the answers explained.
for Loops
A for loop header has three parts: the initialization runs once, the condition is checked before each pass, and the update runs after each pass of the body. The variable it sets up is the loop control variable, and any for loop can be rewritten as an equivalent while loop (and the other way around).
Key terms
forloop- initialization
- update
- loop control variable
A few quick questions on this topic, with the answers explained.
Some loop algorithms come up again and again: checking whether one number divides evenly into another (n % d == 0), pulling out the digits of an integer with % 10 and / 10, counting how often something happens, finding a minimum or maximum, and computing a sum or average. Learn these patterns so you can adapt them quickly on free-response questions.
Key terms
- divisibility with
% - digit extraction
- counter
- minimum and maximum
- sum and average
A few quick questions on this topic, with the answers explained.
String algorithms loop through a string's indexes and use substring to look at pieces of it. Standard examples are checking whether substrings have some property, counting the substrings that meet a condition, and building a reversed copy of a string.
Key terms
- string traversal
substring- counting substrings
- reversing a string
A few quick questions on this topic, with the answers explained.
Nested Iteration
Nested iteration means a loop inside another loop. For each single pass of the outer loop, the inner loop runs all of its passes before the outer loop moves on, which is why nested loops are used for patterns, grids and comparing every pair of items.
Key terms
- nested loop
- inner loop
- outer loop
A few quick questions on this topic, with the answers explained.
A statement execution count is how many times a statement runs, and you find it by tracing the loops. For example, a statement inside two nested loops that each run n times executes n * n times. Comparing these counts is an informal way to say which code segment does more work.
Key terms
- statement execution count
- tracing
- run-time comparison
A few quick questions on this topic, with the answers explained.