AP Computer Science A · Unit 2 · 25-35% of the exam · about 100 minutes

Unit 2: Selection and Iteration

By the end: Trace and write code that makes decisions and repeats work: if statements, Boolean logic, while and for loops, the standard loop algorithms, String traversals and nested loops, and count how many times a statement runs.

Mr. Thompkins
  1. 2.1 Algorithms with selection and repetition · 2.2 Boolean expressions · 8 min

    Questions with true or false answers

    • Three building blocks: sequencing, selection (choose), repetition (repeat).
    • Relational operators ==, !=, <, >, <=, >= produce a boolean.
    • n % d == 0 tests divisibility. n % 2 == 0 tests even.
  2. 2.3 if statements · 8 min

    Choosing what runs

  3. 2.4 Nested if statements · 8 min

    Decisions inside decisions

    • An else-if chain runs only the block of its first true test, then skips the rest.
    • Order the tests from most specific to least specific. A test after a broader one may never run.
    • A final else catches everything left. Without it, a chain can run nothing.
  4. 2.5 Compound Boolean expressions · 9 min

    And, or, not, and short-circuiting

    • && needs both true. || needs at least one. ! flips.
    • Precedence: !, then &&, then ||. Relational operators come before all three.
    • Ranges: x >= lo && x <= hi. Outside a range: x < lo || x > hi.
  5. 2.6 Comparing Boolean expressions · 9 min

    Same answer for every input

    • Equivalent means same value for every input. Prove with a truth table; disprove with one counterexample.
    • De Morgan: !(a && b) is !a || !b; !(a || b) is !a && !b.
    • Negated comparisons: !(x < y) is x >= y; !(x == y) is x != y.
  6. 2.7 while loops · 9 min

    Repeat while it's true

    • A while loop checks its condition before every pass. False at the start means zero passes.
    • The body must move the loop toward stopping, or it's an infinite loop.
    • Trace with a table: one row per pass, and check the condition after every update.
  7. 2.8 for loops · 8 min

    Counting loops on one line

    • for (init; condition; update): init once, condition before every pass, update after every pass.
    • for (int i = a; i < b; i++) runs b - a times; with <=, b - a + 1 times.
    • A variable declared in the header doesn't exist after the loop.
  8. 2.9 Implementing selection and iteration algorithms · 10 min

    The patterns you'll write again and again

    • Every pattern: set up before the loop, update inside it, use the result after it.
    • Start a max or min at the first value (or Integer.MIN_VALUE / Integer.MAX_VALUE), never at 0.
    • Any: return true inside the loop, false after it. All: return false inside, true after.
  9. 2.10 Implementing String algorithms · 9 min

    Walking through a String

    • Traverse with for (int i = 0; i < s.length(); i++) and s.substring(i, i + 1).
    • Build new Strings from "", gluing with +. Glue on the front to reverse.
    • A k-letter window: substring(i, i + k), with i <= s.length() - k.
  10. 2.11 Nested iteration · 8 min

    Loops inside loops

    • The inner loop runs all its passes for every single pass of the outer loop.
    • Trace one outer value at a time, listing every inner value.
    • Watch inner bounds that use the outer variable (j = i, col <= row).
  11. 2.12 Informal run-time analysis · 8 min

    Counting how much work a loop does

  12. Unit 2 wrap-up · 10 min

    Put it together