7m left·0%
Reading Time: 7 min
Last Updated: February 26, 2026
Main Ideas: 4
Reading Time: 7 min
Last Updated: February 26, 2026
Main Ideas: 4

Topic 2.9 Notes – Implementing Selection and Iteration Algorithms

Verified for 2027 AP® Computer Science A Exam
Read aloud
This guide covers the classic loop-and-if algorithms you’re expected to write without thinking. These patterns combine iteration, selection, and a few key variables like counters and accumulators. You’ll use them constantly in FRQs and trace them in multiple choice.

1. What Standard Selection and Iteration Algorithms Are

These algorithms all follow the same structure:

  • A loop (for or while)
  • An if statement
  • One or more tracking variables
    • Counter
    • Running total (accumulator)
    • Current min or max

Here’s the mental template most of them follow:

initialize tracker(s)

loop through data {
    if (condition) {
        update tracker(s)
    }
}

return or print result

If you understand that skeleton, the five required patterns are just variations of it.

2. The Five Core Algorithm Patterns

A. Divisibility Checking

The key tool is the modulo operator %.

  • a % b == 0 → a is evenly divisible by b
  • Even number → n % 2 == 0
  • Multiple of 5 → n % 5 == 0

Example inside a loop:

int count = 0;
for (int i = 1; i <= 30; i++) {
    if (i % 4 == 0) {
        count++;
    }
}

What’s happening:

  • Loop through values
  • Use % to test divisibility
  • Increment a counter if true

Two things graders love to test:

  • Forgetting <= when the endpoint should be included
  • Dividing by 0 if the divisor comes from a variable

B. Digit Extraction

To work with digits of an integer, combine:

  • n % 10 → last digit
  • n / 10 → removes last digit (integer division)

Here’s what that looks like when processing 5824:

Digit extraction using % 10 and / 10

Each loop grabs the last digit, then shortens the number. The digits come off in this order: 4, 2, 8, 5.

Standard pattern:

while (n > 0) {
    int digit = n % 10;
    // process digit
    n /= 10;
}

Used for:

  • Summing digits
  • Counting how many times a digit appears
  • Checking if a number contains a specific digit

Edge cases:

  • n == 0 (loop never runs)
  • Negative numbers → n = Math.abs(n);

Digits are processed right to left. That matters if order is important.

C. Frequency Counting

This is just counting how often a condition is true.

Pattern:

int count = 0;

for (int i = 0; i < arr.length; i++) {
    if (arr[i] > 50) {
        count++;
    }
}

Structure:

  1. Start counter at 0
  2. Check condition
  3. Increment only when true

This shows up constantly. Sometimes you combine it with other logic. For example, count how many times the maximum appears. That means updating the max and possibly resetting the count when a new max is found.

D. Finding Minimum and Maximum

Most common approach is initializing to the first element:

int max = arr[0];

for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
        max = arr[i];
    }
}

Why start at index 1?
Because index 0 is already stored in max.

Alternative approach:

  • int min = Integer.MAX_VALUE;
  • int max = Integer.MIN_VALUE;

That version works well when you’re building the value from scratch.

Watch for:

  • Empty arrays → arr[0] will crash
  • Resetting frequency if tracking both max and count

E. Computing Sum and Average

The accumulator pattern:

int sum = 0;

for (int i = 0; i < arr.length; i++) {
    sum += arr[i];
}

Average requires division:

double avg = (double) sum / arr.length;

Critical issue: integer division truncates.

int sum = 7;
int count = 2;

double avg = sum / count;   // 3.0 (wrong)
double avg2 = (double) sum / count;  // 3.5 (correct)

Also make sure arr.length is not 0 before dividing.

3. Writing and Applying These on the Exam

When building one from scratch, always decide:

  • What variables track the result?
  • Where are they initialized?
  • When exactly do they change?

When tracing, go iteration by iteration. Write a small table if needed. Most mistakes happen when students mentally skip loop iterations.

Combined problems are common:

  • Find max and count its frequency
  • Compute sum and count passing values
  • Extract digits and check a condition

It’s always just multiple trackers inside the same loop.

4. Common Mistakes That Lose Points

Off-by-One Errors

  • i < 10 vs i <= 10
  • Starting at 0 when you meant 1
  • Skipping the last array element

Integer Division

If both operands are int, the result is int. Cast or use a decimal literal.

Edge Cases

  • Empty arrays
  • Single-element arrays
  • All values equal
  • Negative numbers in digit problems
  • Input of 0

AP questions love edge conditions because they expose weak logic.

Key Takeaways

Every one of these algorithms is just loop + if + tracker variables.
a % b == 0 is the entire divisibility test.
Digit extraction always uses % 10 and / 10 together.
When finding max and frequency together, reset the count when a new max appears.
Cast to double before dividing if you need a decimal result.
Most lost points come from loop boundaries and empty-data edge cases, not the main idea.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining