Skip to main content

Unit 2

25–35% of exam

Selection 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 sheet

Free-response questions on this unit

Write your own answer, then score it with the rubric or with AI.

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.

  • AP Computer Science A Unit 2 Review | Selection and Iteration (AP CSA Exam Prep)

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • 2026 AP Computer Science A Exam Review - CRASH COURSE for Unit 2: Selection and Iteration

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA Unit 2 Full Review | NEW 2026 |

    Scratch SkillsWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Unit 4: Iteration and Loops

    Bill BarnumWatch on YouTube (opens in a new tab)

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
  • AP Computer Science A - Topic 2.1: Algorithms with Selection and Repetition

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Algorithms with Selection and Repetition

    CodeHSWatch on YouTube (opens in a new tab)

  • Computer Science Basics: Sequences, Selections, and Loops

    LearnFreeWatch on YouTube (opens in a new tab)

  • Programming Basics: Statements & Functions: Crash Course Computer Science #12

    CrashCourseWatch on YouTube (opens in a new tab)

Read the review notes: 2.1 Algorithms with Selection and Repetition

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
Read the review notes: 2.2 Boolean Expressions

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

  • if statement
  • if-else
  • one-way selection
  • two-way selection
  • flow of control
Read the review notes: 2.3 if Statements

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
  • AP Computer Science A - Topic 2.4 - Part 1: Nested If Statements

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA Selection and Iteration – Nested Conditionals

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • if else if Statements

    CodeHSWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.4 - Part 2: Nested If Statements

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Nested if, else if, and else (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

Read the review notes: 2.4 Nested if Statements

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
  • AP Computer Science A - Topic 2.5 - Part 1: Compound Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CS A 3.5 - Compound Boolean Expressions

    CodeHSWatch on YouTube (opens in a new tab)

  • AP CSA - Unit 3: If Statements - Lesson 4: Short Circuit Evaluation

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.5 - Part 2: Compound Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Java logical operators ❗ (AND OR NOT)

    Bro CodeWatch on YouTube (opens in a new tab)

Read the review notes: 2.5 Compound Boolean Expressions

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
  • equals method
  • AP Computer Science A - Topic 2.6 - Part 1: Comparing Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CS A 3.6 - Equivalent Boolean Expressions

    CodeHSWatch on YouTube (opens in a new tab)

  • De Morgan's Laws For Computer Science and Programming

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.6 - Part 2: Comparing Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA - Unit 3: If Statements - Lesson 6: Comparing Objects

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • .equals() vs. == in Java - The Real Difference

    Coding with JohnWatch on YouTube (opens in a new tab)

Read the review notes: 2.6 Comparing Boolean Expressions

A few quick questions on this topic, with the answers explained.

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
  • while loop
  • loop condition
  • infinite loop
  • off-by-one error
  • AP Computer Science A - Topic 2.7 - Part 1: While Loops

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • While loops (AP CSA)

    CodeHSWatch on YouTube (opens in a new tab)

  • While Loop Java Tutorial #37

    Alex LeeWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.7 - Part 2: While Loops

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.7 - Part 3: While Loops

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Learn Java while loops in 12 minutes! ♾️

    Bro CodeWatch on YouTube (opens in a new tab)

Read the review notes: 2.7 while Loops

A few quick questions on this topic, with the answers explained.

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

  • for loop
  • initialization
  • update
  • loop control variable
Read the review notes: 2.8 for Loops

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
  • AP CSA Unit 2 Loops (2025)

    PickcodeWatch on YouTube (opens in a new tab)

  • 2026 AP Computer Science A Exam Review - Exploring FRQ 1: Methods and Control Structures (Revised)

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Java Program #12 - Reverse a Number in Java

    Programming For BeginnersWatch on YouTube (opens in a new tab)

  • Find Sum of Digits of a Number - Java Code

    Programming TutorialsWatch on YouTube (opens in a new tab)

Read the review notes: 2.9 Implementing Selection and Iteration Algorithms

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
Read the review notes: 2.10 Implementing String Algorithms

A few quick questions on this topic, with the answers explained.

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
Read the review notes: 2.11 Nested Iteration

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
Read the review notes: 2.12 Informal Run-Time Analysis

A few quick questions on this topic, with the answers explained.