3.5

Compound Booleans

AP Computer Science A

The three logical operators

AND operator (&&)

java
int age = 20;
boolean hasLicense = true;

if (age >= 16 && hasLicense) {
    System.out.println("Can drive");  // prints
}

AND truth table

java
int score = 85;
int attendance = 92;

// Both must be high for honors
if (score >= 90 && attendance >= 90) {
    System.out.println("Honors");
}
// 85 >= 90 is false → entire expression is false → skipped

OR operator (||)

java
int day = 6;  // Saturday

if (day == 6 || day == 7) {
    System.out.println("Weekend!");  // prints
}

OR truth table

java
String weather = "rain";

if (weather.equals("rain") || weather.equals("snow")) {
    System.out.println("Bring umbrella");
}

NOT operator (!)

java
boolean isRaining = false;

if (!isRaining) {
    System.out.println("Go outside!");  // prints because !false = true
}

NOT truth table

java
int age = 15;

if (!(age >= 18)) {
    System.out.println("Minor");  // prints: !(15 >= 18) → !(false) → true
}

Short-circuit evaluation

AND short-circuits on false

java
// If the left side is false, Java doesn't check the right side
int x = 0;
if (x != 0 && 10 / x > 2) {  // x != 0 is false → stops here
    System.out.println("Yes");
}
// No ArithmeticException because 10/x is never evaluated!

OR short-circuits on true

java
// If the left side is true, Java doesn't check the right side
String s = null;
if (s == null || s.length() == 0) {  // s == null is true → stops here
    System.out.println("Empty or null");
}
// No NullPointerException because s.length() is never called!

Combining operators

Operator precedence

java
// Without parentheses: ! first, then &&, then ||
boolean result = !true || false && true;
// Step 1: !true → false
// Step 2: false && true → false
// Step 3: false || false → false
java
if ((age >= 13 && age <= 19) || isStudent) {
    // teenager OR student
}

Range checking

java
// Check if x is between 1 and 100 (inclusive)
if (x >= 1 && x <= 100) {
    System.out.println("In range");
}

// Check if x is OUTSIDE 1 to 100
if (x < 1 || x > 100) {
    System.out.println("Out of range");
}

Tracing compound expressions

java
int a = 5, b = 10, c = 3;

boolean r1 = (a > c) && (b > a);       // true && true → true
boolean r2 = (a > b) || (c < a);       // false || true → true
boolean r3 = !(a == 5);                // !(true) → false
boolean r4 = (a > b) && (c > 0);       // false && true → false
boolean r5 = (a != c) || (b == 10);    // true || true → true

Complete example: Amusement park ride

java
public class RideChecker {
    public static void main(String[] args) {
        int height = 54;    // inches
        int age = 10;
        boolean hasParent = true;
        
        // Must be tall enough AND (old enough OR with parent)
        if (height >= 48 && (age >= 12 || hasParent)) {
            System.out.println("You can ride!");
        } else {
            System.out.println("Sorry, can't ride.");
        }
    }
}

AP Exam Tips

Common Mistakes

Key Vocabulary