Bright Sprouts AcademyColumbia & Imperial educators

AP Computer Science A · Topic notes

Selection and Iteration

Key ideas, common mistakes, and a worked example for Selection and Iteration in AP Computer Science A.

Practice Selection and Iteration

13 untimed questions from the free practice test.

Exam weighting: 25–35%

Selection and Iteration is about making decisions and repeating work: boolean expressions, if statements, while and for loops, nested loops, string algorithms, and counting how many times a statement runs. It carries 25–35% of the multiple-choice section, and every free-response question relies on it.

Key ideas

  • && and || short-circuit: Java stops evaluating as soon as the result is known, which can prevent errors such as dividing by zero.
  • De Morgan's laws: !(a && b) is !a || !b, and !(a || b) is !a && !b. Negate each comparison too, so the negation of x > 5 is x <= 5.
  • && has higher precedence than ||. Add parentheses whenever a condition mixes the two.
  • Trace loops with a table of variable values. Check the first pass, the last pass, and the moment the condition fails.
  • To count statement executions in nested loops, multiply when the inner loop always runs the same number of times, and add up the passes when it depends on the outer variable.

Common mistakes

  • Treating separate if statements as one if–else if chain.
  • Off-by-one loop bounds, such as < where <= was needed.
  • Negating only one part of a compound condition, or keeping && when De Morgan's law calls for ||.
  • Forgetting to update the loop variable in a while loop, which makes the loop run forever.

Worked example

How many times is the statement total++; executed?

int total = 0;
for (int i = 0; i < 4; i++)
{
    for (int j = i; j < 4; j++)
    {
        total++;
    }
}

Solution

The inner loop starts at j = i, so it runs 4 times when i is 0, 3 times when i is 1, 2 times when i is 2, and once when i is 3. The statement runs 4 + 3 + 2 + 1 = 10 times.

Stuck on Selection and Iteration?

A tutor can find the gap in one session. Book a free consultation.

Every question on this site is original and written by Bright Sprouts Academy. AP, Advanced Placement, SAT, and PSAT/NMSQT are registered trademarks of College Board, which is not affiliated with and does not endorse this site. PSAT/NMSQT is also a trademark jointly owned by College Board and National Merit Scholarship Corporation.

Last updated . Exam board and university requirements change, so always check the official specification and admissions pages.