Unit 2: Selection and Iteration
A plain-language study guide to AP Computer Science A Unit 2, Selection and Iteration: all 34 ideas across 12 topics, each explained for students.
Everything the AP Computer Science A exam can ask about in Unit 2, explained in plain language. This unit is roughly 25–35% of the multiple-choice section and has 12 topics and 34 ideas.
Each idea has a code like 1.2.A.1: the unit, the topic, the learning objective (the letter), then the idea itself.
The codes on the practice questions link straight to the idea they tested. These are summaries in our own words.
For the exact official wording, see the
AP Computer Science A Course and Exam Description.
Topics in this unit
- 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
Topic 2.1: Algorithms with Selection and Repetition
Practice this topic: Topics 2.1–2.3: Boolean Expressions and if Statements
2.1.A — Describe everyday patterns and processes that involve making choices and repeating steps, using written steps or diagrams.
- 2.1.A.1 Every algorithm (a step-by-step plan for solving a problem) is made from three basic pieces: sequencing (steps in order), selection (choosing a path), and repetition (doing steps again).
- 2.1.A.2 An algorithm can make decisions, which is called selection, and it can go around again and again, which is called repetition or looping.
- 2.1.A.3 Selection means the algorithm picks which way to go next by checking something that is either true or false, like "is it raining?"
- 2.1.A.4 Repetition means a process keeps running over and over until you get the result you were aiming for, like stirring until the sugar dissolves.
- 2.1.A.5 Changing the order you place sequencing, selection, and repetition steps can change what the algorithm produces, so the arrangement matters, not just which pieces you use.
Topic 2.2: Boolean Expressions
Practice this topic: Topics 2.1–2.3: Boolean Expressions and if Statements
2.2.A — Write code that builds boolean expressions with relational operators, and figure out whether each one is true or false.
-
2.2.A.1
==and!=check whether two values are the same or different. For primitive types (likeintordouble), they compare the actual values. For reference types (objects), they check whether both sides point to the very same object. -
2.2.A.2 For numbers,
<,>,<=, and>=tell you how two values relate: less than, greater than, less than or equal, or greater than or equal. -
2.2.A.3 Any expression that uses a relational operator (like
<or==) gives back abooleanresult, eithertrueorfalse.
Topic 2.3: if Statements
Practice this topic: Topics 2.1–2.3: Boolean Expressions and if Statements
2.3.A — Write code that branches using if and if-else statements, and predict what that code does.
- 2.3.A.1 Normally code runs top to bottom, one line after another. Selection statements break that pattern by letting the program skip some code or choose between pieces of code.
-
2.3.A.2 An
ifstatement is one kind of selection statement. It controls what runs by checking abooleanexpression, and different code runs depending on whether that expression istrueorfalse. -
2.3.A.3 A one-way selection is a plain
if, used when some code should run only under one condition. The body (the code in the braces) runs only when thebooleanexpression istrue. Otherwise it is skipped. -
2.3.A.4 A two-way selection is an
if-else, used when you have one chunk of code for each outcome. If thebooleanexpression istrue, theifbody runs. If it isfalse, theelsebody runs instead.
Topic 2.4: Nested if Statements
Practice this topic: Topics 2.4–2.6: Nested if and Compound Booleans
2.4.A — Write code with if statements placed inside other if statements, and predict the outcome.
-
2.4.A.1 Nested
ifstatements areif,if-else, orif-else-ifstructures placed inside the body of anotherif,if-else, orif-else-if. -
2.4.A.2 In nested
ifstatements, the inner condition gets checked only when the outer condition istrue. If the outer one isfalse, the inner one never even runs. -
2.4.A.3 A multiway selection (
if-else-if) handles several conditions, each with its own code. Java checks them in order and runs only the code for the firsttrueone, at most one block in total. If none aretrueand there is a finalelse, thatelsecode runs.
Topic 2.5: Compound Boolean Expressions
Practice this topic: Topics 2.4–2.6: Nested if and Compound Booleans
2.5.A — Write compound boolean expressions with !, &&, and ||, and work out whether they are true or false.
-
2.5.A.1 Logical operators combine
booleanexpressions, and the result is always aboolean.!aflips the value:trueifaisfalse, otherwisefalse.a && b(and) istrueonly when both aretrue.a || b(or) istruewhen at least one istrue, including when both are.- Java works out
!first, then&&, then||.
-
2.5.A.2 Short-circuit evaluation is when Java stops early because the first part of an
&&or||already settles the answer. When that happens, the second part is never evaluated.
Topic 2.6: Comparing Boolean Expressions
Practice this topic: Topics 2.4–2.6: Nested if and Compound Booleans
2.6.A — Decide whether two boolean expressions are equivalent, using tools like truth tables and De Morgan's law.
-
2.6.A.1 Two
booleanexpressions are equivalent when they always produce the same result, no matter what values go in. A truth table (a chart listing every possible combination of inputs) can prove it. -
2.6.A.2 De Morgan's law gives you a way to rewrite a
booleanexpression into an equivalent one.!(a && b)is the same as!a || !b.!(a || b)is the same as!a && !b.
2.6.B — Write code that compares object references with boolean expressions, and predict the result.
-
2.6.B.1 Two different variables can hold references to one and the same object. You can use
==and!=to compare object references. -
2.6.B.2 You can compare a reference to
nullwith==or!=to find out whether the variable actually refers to an object or refers to nothing. -
2.6.B.3 A class can define its own
equalsmethod to decide when two of its objects count as equal, usually by comparing their attributes (the data they hold). Writing your ownequalsmethod is not something you are tested on.
Topic 2.7: while Loops
Practice this topic: Topics 2.7–2.8: while and for Loops
2.7.A — Recognize situations where you need a loop to repeat work in order to get the result you want.
-
2.7.A.1 Iteration is a kind of repetition. An iteration statement (a loop) runs a block of code zero or more times, and keeps going as long as its controlling
booleanexpression staystrue. -
2.7.A.2 An infinite loop never ends, because the loop's
booleanexpression istrueevery time it is checked. -
2.7.A.3 If the loop's
booleanexpression isfalsethe very first time it is checked, the loop body never runs at all. - 2.7.A.4 An off-by-one error means a loop repeats one more time, or one fewer time, than you meant it to.
2.7.B — Write while loops to repeat a process, and predict what those loops produce.
-
2.7.B.1 A
whileloop is one kind of iteration statement. Java checks thebooleanexpression before every pass through the body, even the first. If it istrue, the body runs. This repeats until the expression isfalse, and then the loop ends.
Topic 2.8: for Loops
Practice this topic: Topics 2.7–2.8: while and for Loops
2.8.A — Write for loops to repeat a process, and predict what those loops produce.
-
2.8.A.1 A
forloop is one kind of iteration statement. Its header has three parts: the initialization (where you set up a counter), thebooleanexpression (the condition to keep going), and the update (how the counter changes). -
2.8.A.2 In a
forloop, the initialization runs just once, at the very start, and sets up the loop control variable (the counter). Then thebooleanexpression is checked, and it is checked again after each update, until it isfalse. In each pass, the update runs after the whole body finishes and before the next check. -
2.8.A.3 Any
forloop can be rewritten as an equivalentwhileloop that does the same job, and anywhileloop can be rewritten as aforloop.
Topic 2.9: Implementing Selection and Iteration Algorithms
Practice this topic: Topics 2.9–2.10: Selection, Iteration, and String Algorithms
2.9.A — Write code for common and original algorithms that use only selection and loops, and predict their results.
-
2.9.A.1 Some algorithms come up so often that they are considered standard. They let you do these jobs.
- Check whether one integer divides evenly into another.
- Pick out the individual digits of an integer.
- Count how often something meeting a certain rule happens.
- Find the smallest or largest value.
- Compute a sum or an average.
Topic 2.10: Implementing String Algorithms
Practice this topic: Topics 2.9–2.10: Selection, Iteration, and String Algorithms
2.10.A — Write code for common and original algorithms that work on Strings, and predict what they produce.
-
2.10.A.1 Some String algorithms are standard ones you will see again and again.
- Check whether one or more substrings (smaller pieces of a String) have a certain property.
- Count how many substrings fit a given rule.
- Build a new String with the characters in backward order.
Topic 2.11: Nested Iteration
Practice this topic: Topics 2.11–2.12: Nested Iteration and Run-Time Analysis
2.11.A — Write loops placed inside other loops, and predict what they do.
- 2.11.A.1 Nested iteration means one loop sits inside the body of another loop. Each time the outer loop goes around once, the inner loop must finish all of its passes before the outer loop moves on to its next pass.
Topic 2.12: Informal Run-Time Analysis
Practice this topic: Topics 2.11–2.12: Nested Iteration and Run-Time Analysis
2.12.A — Count how many times statements run in loops, and use those counts to roughly compare how long different loops take.
- 2.12.A.1 A statement execution count is how many times a particular line of code runs. You can usually work it out informally by tracing through the loops and figuring out how often each one repeats.