Unit 2.5

Compound Boolean Expressions

AP Computer Science A

Learning to combine Boolean expressions using logical operators

Learning Objectives:

2.5.A: Develop code to represent compound Boolean expressions and determine the result of these expressions.

Logical Operators

Logical operators allow us to combine simple Boolean expressions into more complex ones.

!

NOT

Reverses the Boolean value

&&

AND

True when both conditions are true

||

OR

True when at least one condition is true

Essential Knowledge 2.5.A.1:

The order of precedence for evaluating logical operators is:

! (not), then && (and), then || (or)

The NOT Operator (!)

The NOT operator reverses a Boolean value. If the expression is true, ! makes it false, and vice versa.

Syntax:

!expression

Examples:

boolean isRaining = true; boolean isNotRaining = !isRaining; // false int score = 85; boolean didNotPass = !(score >= 60); // false

Truth Table:

a !a
true false
false true

The AND Operator (&&)

The AND operator returns true only when both expressions are true.

Syntax:

expression1 && expression2

Examples:

int age = 16; boolean hasLicense = true; boolean canDrive = (age >= 16) && hasLicense; // true int temperature = 75; boolean perfectWeather = (temperature > 70) && (temperature < 80); // true

Truth Table:

a b a && b
true true true
true false false
false true false
false false false

The OR Operator (||)

The OR operator returns true when at least one of the expressions is true.

Syntax:

expression1 || expression2

Examples:

boolean isWeekend = true; boolean isHoliday = false; boolean noSchool = isWeekend || isHoliday; // true char grade = 'B'; boolean passedClass = (grade == 'A') || (grade == 'B') || (grade == 'C'); // true

Truth Table:

a b a || b
true true true
true false true
false true true
false false false

Order of Operations

When multiple logical operators are used, Java follows a specific order of precedence:

Precedence (highest to lowest):

  1. ! (NOT) - evaluated first
  2. && (AND) - evaluated second
  3. || (OR) - evaluated last

Examples:

boolean a = true; boolean b = false; boolean c = true; // Without parentheses: ! evaluated first, then &&, then || boolean result1 = a || b && !c; // Equivalent to: a || (b && (!c)) // true || (false && false) = true || false = true // With parentheses to change order: boolean result2 = (a || b) && !c; // (true || false) && false = true && false = false
Tip: Use parentheses to make your intentions clear and avoid confusion!

Short-Circuit Evaluation

Essential Knowledge 2.5.A.2: Short-circuit evaluation occurs when the result of a logical operation can be determined by evaluating only the first Boolean expression.

AND (&&) Short-Circuit:

// If first expression is false, second is not evaluated boolean result = false && someMethod(); // someMethod() is NOT called

OR (||) Short-Circuit:

// If first expression is true, second is not evaluated boolean result = true || someMethod(); // someMethod() is NOT called

Practical Example:

String text = null; // Safe because if text is null, text.length() is never called if (text != null && text.length() > 0) { System.out.println("Text has content"); }

Why is this important?

  • Prevents errors (like NullPointerException)
  • Improves performance by avoiding unnecessary operations
  • Allows for safe conditional checking

Interactive Demo

Try different combinations and see the results:

Click a button to see the result!

Try These Combinations:

  • A = true, B = false: What does A && B return?
  • A = false, B = true: What does A || B return?
  • A = true, B = false: What does !A && B || A return?

Common Patterns

Range Checking:

int score = 85; boolean isValidScore = score >= 0 && score <= 100;

Multiple Conditions:

char grade = 'A'; boolean isPassingGrade = grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D';

Exclusion Pattern:

int day = 3; // Wednesday boolean isNotWeekend = !(day == 1 || day == 7); // Not Sunday or Saturday

Safe Navigation:

String name = getName(); if (name != null && !name.equals("")) { System.out.println("Hello, " + name); }

Best Practices:

  • Use parentheses for clarity
  • Put simpler conditions first for short-circuiting
  • Check for null before calling methods

Practice Problems

Problem 1:

What is the result of each expression?

boolean x = true; boolean y = false; boolean z = true; a) !x && y || z b) x || !y && z c) !(x && y) || z

Problem 2:

Write a compound Boolean expression for each condition:

  • A student passes if their score is 60 or above AND they attended at least 80% of classes
  • A movie is suitable if it's rated G OR it's rated PG and the viewer is 10 or older
  • A password is strong if it's at least 8 characters AND contains both letters and numbers
Think about: Which operator precedence rules apply? Where might short-circuit evaluation occur?

Unit 2.5 Review

Key Concepts:

  • ! (NOT): Reverses Boolean value
  • && (AND): True only when both expressions are true
  • || (OR): True when at least one expression is true
  • Precedence: ! then && then ||
  • Short-circuit evaluation: Second expression may not be evaluated

Remember:

// Order matters! boolean result1 = a || b && c; // a || (b && c) boolean result2 = (a || b) && c; // Different result! // Short-circuit is your friend if (obj != null && obj.method()) { // Safe to call obj.method() }
Next: Apply these concepts in conditional statements and loops!
1 / ?