1. LxD Cycle Process

Empathize: Students write conditions that read correctly in English but group incorrectly in Java, because they assume && and || have equal precedence. The result compiles and returns wrong answers for some inputs only.

Define:

  • POV: CSA students need to apply precedence deliberately, because a compound condition can be right for the values they tested and wrong for the ones they did not.
  • Learning Goal: Students will build truth tables, evaluate compound conditions, apply precedence and predict which side short circuit evaluation skips.

Ideate:

  • HMW Question: How might we surface the inputs where a mis-grouped condition actually fails, instead of the ones where it happens to work?
  • Activity: A truth table builder, an admission rule with several input combinations, a precedence and short circuit exercise, and a sorting task.

Prototype & Test: Activity 2 varies every input combination rather than a single example, because a mis-grouped condition is correct for some inputs and only fails on others.


2. 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 (5 minutes)

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.

3. 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.

4. 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 ...

5. Hacks & Practice Tasks

Prepare your submission IPYNB

  1. Create a new notebook in your portfolio homework area: _notebooks/homework.
  2. Add one markdown cell at the top with the frontmatter:
---
layout: post
title: CSA Unit 2.05 Compound Boolean Expressions HW
categories: [Java, Compound-Boolean-Expressions]
lesson_language: Java
lesson_topic: Compound-Boolean-Expressions HW
lesson_source: APCSA
lesson_type: lesson
permalink: /csa/unit_02/2_5_hw
author: yourGithubID
---
  1. Add code cells for the Popcorn Hack and the Homework Hack. Ensure all code runs and output is visible.
  2. Include a markdown cell before each code section explaining the concept.

Submission Safety Rules (Read First)

[IMPORTANT] To avoid grading errors, follow these rules exactly:

  • Write your prediction before you run each code section
  • Execute every code cell and leave the output visible
  • Show your work for any hand-traced prediction
  • Do not just copy code; explain what each line does
  • Label each question clearly

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 (In-Class)

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.

6. Grading Plan (1 Point Total)

Classroom Rubric

  • 0.2 points: Popcorn completion Student fixed both the precedence grouping and the reversed member check, and tested the combinations listed in the runner.

  • 0.8 points: Homework completion

    • 0.5 Leap year rule: The condition implements divisible by 4 and not by 100, or divisible by 400, in a single Boolean expression.
    • 0.2 Boundary tests: The rule is tested against 1900, 2000, 2024 and 2023.
    • 0.1 Notebook quality: Every code cell executed with output visible, and a markdown cell before each code section explaining the concept.

Quick Validation Checklist

  • Present: parentheses that make the grouping explicit
  • Present: all four leap year test values checked
  • Absent: a condition that relies on default precedence to be readable
  • Test: 1900 is not a leap year and 2000 is

7. Lesson Revisions & Feedback Evidence

Feedback Received: Team review of the Unit 2 set found that these lessons did not follow the section format used by the Unit 1 lessons, and that they ended without the assignment submission form every other CSA lesson provides.

Revision Made: The lesson was reorganised into the shared CSA lesson format, so it now opens with the LxD cycle and lesson plan, states its reference material before the code, and groups the activities, Popcorn Hack and Homework Hack under one practice section. The duplicate lesson heading was removed, and the page now renders the standard Submit Assignment form at the end.

Design Decision Kept: Activity 2 varies every input combination rather than a single example, because a mis-grouped condition is correct for some inputs and only fails on others.

Open Item: Record peer feedback from the team teach delivery here once the lesson has been taught.


8. Summary

  • && needs both sides true, || needs at least one, ! flips.
  • Precedence: !, then &&, then ||. Parentheses remove all doubt.
  • Short circuit: && stops on a false left side, || stops on a true left side. Use it to guard against errors like dividing by zero.
  • Next lesson: when two different expressions mean the same thing.

9. References

College Board Course and Exam Description

Topic 2.5, Compound Boolean Expressions, is required content on the AP Computer Science A Exam. The objective quoted in section 2 of this lesson 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

Submit Assignment

Click to upload or drag and drop
PDF, ZIP, images, documents, or Jupyter notebooks (.ipynb) (Max 10MB per file)

Need to update a submission later? Open the submissions dashboard.