01
Overview

Combining modulo with range checks.

Test divisibility and bounds in one compound condition — the building block of "every nth in range" problems.

Modulo (%) tests divisibility: n % k == 0 is true when k divides n exactly. Range conditions bound a value between a lower and upper limit.

Together they answer questions like "count multiples of 3 between 10 and 50" or "list even numbers from 1 to 100". The combined boolean uses && to chain both tests.

On the AP exam, this combination appears in tracing MCQs and in FRQs that filter by divisibility within a range.

02
Core Concept

One compound condition handles both tests.

&& chains divisibility with bounds.

The template

if (n % k == 0 && n >= lo && n <= hi) {
    // n is a multiple of k in [lo, hi]
}

Concrete example: count multiples of 3 from 1 to 100

int count = 0;
for (int n = 1; n <= 100; n++) {
    if (n % 3 == 0) count++;
}
// count = 33

Counting only when both pass

public int countMultiplesInRange(int[] arr, int k, int lo, int hi) {
    int count = 0;
    for (int n : arr) {
        if (n % k == 0 && n >= lo && n <= hi) {
            count++;
        }
    }
    return count;
}

Inclusive vs exclusive bounds

  • n >= lo includes the lower endpoint.
  • n <= hi includes the upper endpoint.
  • Use > or < for exclusive endpoints.
03
Key Vocabulary

Modulo + range vocabulary.

Used in divisibility-filter problems.

Vocabulary
  • Modulo (%) — operator returning the remainder.
  • Divisibilityn % k == 0 means k divides n.
  • Range condition — bounded by lo and hi.
  • Inclusive — endpoint included via >= / <=.
  • Exclusive — endpoint excluded via > / <.
  • Compound condition — multiple tests joined by && or ||.
04
AP Exam Focus

How modulo + range is tested.

Endpoint inclusion and modulo on negatives dominate.

Exam Focus
  • Inclusive vs exclusive boundary. "Between 10 and 20" usually includes both.
  • Modulo on negatives. -7 % 3 is -1 in Java, not 2.
  • Order of operands. 0 == n % k works too, but n % k == 0 reads naturally.
  • && short-circuits. If the modulo check fails, range check is skipped.
05
Worked Example

Count multiples of 5 in [20, 40].

Compound condition in action.

Worked Example AP-style reasoning

Input: [5, 25, 30, 38, 40, 45]; k=5; lo=20; hi=40.

Trace.

  • 5: % 5 == 0 yes; 5 ≥ 20 no → skip.
  • 25: % 5 == 0 yes; 25 in [20, 40] yes → count.
  • 30: yes and yes → count.
  • 38: % 5 == 0 no → skip.
  • 40: yes and yes → count.
  • 45: yes; 45 ≤ 40 no → skip.

Answer: 3.

06
Common Mistakes

Modulo and range pitfalls.

Most are endpoint or operator errors.

Watch Out
  • Wrong endpoint inclusion. "Less than 50" excludes 50; "at most 50" includes it.
  • Negative modulo confusion. Java keeps the sign of the dividend.
  • Using || instead of &&. Wrong logic — must satisfy both.
  • Comparing n % k = 0. Assignment, not comparison; use ==.
07
Reference Table

Modulo + range at a glance.

Boundary inclusion summary.

Modulo & Range Reference

AP Quick Reference
TestCodeMeaning
Divisiblen % k == 0k divides n.
Inclusive rangen >= lo && n <= hiBoth ends included.
Half-openn >= lo && n < hiHi excluded.
Combinedn % k == 0 && rangeBoth must hold.
08
Practice Tip

How to master compound conditions.

Underline the prompt's boundary words.

Practice Strategy
  • MCQ Practice — check both sides of the AND; check endpoint inclusion.
  • Java Lab — count multiples in ranges; test with negative numbers.
  • FRQ Practice — write the boundary words into a comment before coding the condition.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 09 Topic 02 Modulo and Range Conditions FRQ Practice

AP-style topic practice assessment

Start