Skip to main content
← AP CSA Study Guide

Code Scholars Tutoring · AP Computer Science A

2.3: Compound Conditions

Use && when all requirements must hold and || when any alternative is sufficient. Java does not support chained comparisons such as 0 < x < 10.

College Board Unit 2

How it works

Compound conditions join several tests into one decision. Range checks need && because a value must satisfy both bounds, while membership checks across separate values need ||. Reading a compound condition well means substituting a concrete value and evaluating each piece, rather than pattern-matching on how the code looks.

Evaluate each comparison to true or false first, then combine, rather than reading the condition as a sentence.

Common error

Chaining comparisons as in mathematics. Java requires each comparison to be written out and joined explicitly.

Worked example

Java

int n = 12;
System.out.println(n >= 10 && n <= 20 && n % 2 == 0);

Output

true

The value meets both inclusive bounds and the evenness test.

Exercises

2.3.1 · Trace

Determine the exact output. Explain the Java rule or state changes that produce it.

Java

int n = 5;
System.out.println(n < 2 || n >= 5 && n < 8);
Show solution

Output

true

The && group is true, making the full disjunction true.

2.3.2 · Write code

Write public static boolean eligible(int age, boolean permission). Return true for adults age 18 or older, or for ages 14 through 17 with permission. Younger applicants are ineligible.

Show solution

One valid solution is shown below. Other implementations satisfying the contract are also valid.

Java

public static boolean eligible(int age, boolean permission) {
    return age >= 18 || (age >= 14 && permission);
}

Check your code

Java

System.out.println(eligible(13, true));
System.out.println(eligible(14, true));
System.out.println(eligible(17, false));
System.out.println(eligible(18, false));

Expected output

false
true
false
true
Practice this topic: MCQ →

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic