Reference Guide

Key Ideas

  • Logical operators combine Boolean values: ! (NOT), && (AND), || (OR).
  • a && b is true only when both are true.
  • a || b is true when at least one is true.
  • !a flips the value: !true is false.
  • Precedence: ! is evaluated first, then &&, then ||. Use parentheses whenever the order is not obvious.
  • Short circuit evaluation: && stops as soon as the left side is false; || stops as soon as the left side is true. The right side is never evaluated in those cases.

Operator Table

a b a && b a \|\| b !a
true true true true false
true false false true false
false true false true true
false false false false true
  • Short circuit makes this safe: if (count != 0 && total / count > 5). When count is 0, the division never runs, so there is no crash.

College Board Requirements

AP CSA Unit 2, Topic 2.5: Compound Boolean Expressions.

This lesson is built directly against the College Board’s stated learning objective for this topic:

Evaluate compound Boolean expressions in program code.

Topic 2.5, Compound Boolean Expressions, is required content on the AP Computer Science A Exam. The learning objective quoted above is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.


Lesson Plan

Learning Objective: Evaluate compound Boolean expressions in program code.

Success Criteria: You can evaluate expressions that combine &&, || and !, apply the precedence rules without guessing, and explain what short circuit evaluation skips.

Tech Talk & Introduction

Compound conditions join simple comparisons into one question.

  • && is true only when both sides are true. || is true when either side is true. ! flips a value.
  • Precedence runs ! first, then &&, then ||. Parentheses override this and usually make the intent clearer.
  • Short circuit evaluation stops as soon as the answer is known, so the right side may never run. That matters when the right side has an effect.

Code Examples

A. Simple: Compound Conditions in Code

Run the program. Then change the values at the top so that every message is different.

Code Runner Challenge

Compound conditions: AND, OR, NOT, and a safe short circuit. Change the values and run again.

View IPYNB Source
// CODE_RUNNER: Compound conditions: AND, OR, NOT, and a safe short circuit. Change the values and run again.
public class CompoundDemo {
    public static void main(String[] args) {
        int age = 16;
        boolean hasPermit = true;
        boolean hasAdult = false;
        int hour = 22;

        // AND: both must be true
        if (age >= 16 && hasPermit) {
            System.out.println("You may practice driving.");
        }

        // OR: at least one must be true
        if (hasAdult || hour < 21) {
            System.out.println("You may drive right now.");
        } else {
            System.out.println("Not now: no adult and it is after 9 pm.");
        }

        // NOT flips the value
        if (!hasPermit) {
            System.out.println("Get a permit first.");
        }

        // Short circuit protects the division
        int trips = 0;
        int miles = 120;
        if (trips != 0 && miles / trips > 20) {
            System.out.println("Long trips on average.");
        } else {
            System.out.println("No trips yet, no average.");
        }
    }
}
CompoundDemo.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Hacks & Practice Tasks

Activity 1: Build the Truth Table

Click each ? to cycle through true and false, then check your table.

Truth Table Builder
Four expressions. Fill in all 16 cells.

Activity 2: Who Gets In?

A ride requires a rider to be at least 48 inches tall AND either at least 12 years old OR with an adult. Change the inputs and watch the condition.

Ride Admission
One compound condition decides everything.

Activity 3: Precedence and Short Circuits

Precedence and Short Circuits
Three questions that show up on the AP exam in some form every year.
Question 1

What is the value of true || false && false?

Question 2

count is 0. What happens when this line runs?

if (count != 0 && total / count > 5) {
System.out.println("High average");
}
Question 3

Which expression is true exactly when x is between 1 and 10, inclusive?

Activity 4: Sort by Value

With a = true, b = false, and x = 8, sort each expression into TRUE or FALSE.

Evaluate the Compound Expressions
Apply precedence: ! first, then &&, then ||.

Click an item, then click a bucket. Or drag it. Click an item inside a bucket to send it back.

TRUE
FALSE

Popcorn Hack

The discount rule is: seniors (65 and up) get a discount, and so do students who are also members. The condition below is wrong in two ways: the precedence is off and the member check is backwards. Fix both (parentheses help) so age = 20, isStudent = true, isMember = false prints No discount, and age = 20, isStudent = true, isMember = true prints Discount!.

Code Runner Challenge

Popcorn hack: fix the compound condition so only seniors, or students who are members, get the discount.

View IPYNB Source
// CODE_RUNNER: Popcorn hack: fix the compound condition so only seniors, or students who are members, get the discount.
public class DiscountRule {
    public static void main(String[] args) {
        int age = 20;
        boolean isStudent = true;
        boolean isMember = false;

        // Bug: precedence. This gives a discount to every student.
        if (age >= 65 || isStudent && isMember == false) {
            System.out.println("Discount!");
        } else {
            System.out.println("No discount");
        }
    }
}
DiscountRule.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack

Two short tasks.

  1. Finish the runner: a year is a leap year when it is divisible by 4 and not by 100, or when it is divisible by 400.
  2. Answer the quick check.

Code Runner Challenge

Homework: leap year rule with one compound Boolean expression.

View IPYNB Source
// CODE_RUNNER: Homework: leap year rule with one compound Boolean expression.
public class LeapYear {
    public static void main(String[] args) {
        int year = 2024;   // test 2024 (leap), 1900 (not), 2000 (leap), 2023 (not)

        // TODO: write the compound condition with &&, ||, and %
        boolean isLeap = false;

        if (isLeap) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}
LeapYear.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework Quick Check
Type true or false.
Question 1

What is the value of !(x > 3) || y == 4 when x = 5 and y = 4?

Question 2

With a = false, does Java evaluate b in a && b? Type yes or no.

References

College Board Course and Exam Description

Topic 2.5, Compound Boolean Expressions, is required content on the AP Computer Science A Exam. The learning objective quoted in section 3 (College Board Requirements) is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.

Outside Academic Reference

The conditional operators are defined to evaluate their right operand only when the result is not already determined by the left operand, which is exactly the short circuit behaviour the lesson demonstrates.

Reference List

College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a

Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.23

All Java lessons