01 / 21
\(\&\&\)
\(||\)
!

Compound Boolean
Conditions

Learn to combine multiple boolean expressions using logical operators to write powerful, precise conditions in Java.

Java AP Computer Science A

What Is a Boolean?

In Java, a boolean is a data type with exactly two possible values: true or false. Every comparison and logical test produces a boolean result.

true
The condition is satisfied. Example: \(5 > 3\) evaluates to true.
false
The condition is not met. Example: \(2 > 7\) evaluates to false.

Why Use Compound Conditions?

Real-world decisions rarely depend on a single test. You might need to check multiple criteria at once: Is the user old enough and do they have a valid ID? Is the temperature below freezing or is it raining?

// Without compound condition: nested and repetitive if (age >= 16) { if (hasPermit) { System.out.println("Can drive"); } } // With compound condition: clear and concise if (age >= 16 && hasPermit) { System.out.println("Can drive"); }

The Three Logical Operators

\(\&\&\) (AND)
true only when both sides are true. Both conditions must be satisfied.
\(||\) (OR)
true when at least one side is true. Either condition suffices.
\(!\) (NOT)
Flips the boolean value. true becomes false and false becomes true.

Truth Table: \(\&\&\) (AND)

A B
\(A \text{ && } B\)
true true true true false false false true false false false false Only ONE row produces true: both must be true

Truth Table: \(||\) (OR)

A B
\(A \text{ || } B\)
true true true true false true false true true false false false Only ONE row produces false: both must be false

The \(!\) (NOT) Operator

The NOT operator inverts a single boolean value. It is a unary operator: it acts on one operand, not two.

// NOT operator examples boolean isSunny = true; System.out.println(!isSunny); // false System.out.println(!(5 > 3)); // false System.out.println(!(2 == 7)); // true

\(\&\&\) vs \(||\): Key Distinction

\(\&\&\) (AND) \(||\) (OR)
Meaning Both must be true At least one must be true
Result is true when true \(\&\&\) true Any operand is true
Result is false when Any operand is false false \(||\) false
Short-circuit skips when Left is false Left is true
Real-world analogy "Must have ticket AND ID" "Cash OR credit accepted"

Operator Precedence

When multiple logical operators appear in one expression, Java evaluates them in a specific order. Just like multiplication before addition in math:

1
\(!\) (NOT): evaluated first (highest precedence).
2
\(\&\&\) (AND): evaluated second.
3
\(||\) (OR): evaluated last (lowest precedence).

How to Write Compound Conditions

1
Identify each condition. Write each simple boolean expression separately first.
2
Choose the operator. Do all conditions need to be true? Use \(\&\&\). Does at least one suffice? Use \(||\).
3
Write each side as a complete expression. Both sides of \(\&\&\) or \(||\) must be valid boolean expressions.
4
Add parentheses for clarity. Group sub-expressions so the meaning is obvious, even when not strictly required.
5
Test edge cases. Check what happens when each condition is true and when it is false.

Example 1: Range Check

Problem: Check if a variable score is between 0 and 100, inclusive.

int score = 85; // Both conditions must be true if (score >= 0 && score <= 100) { System.out.println("Valid score"); } else { System.out.println("Invalid score"); }

Example 2: Eligibility Check

Problem: A person can vote if they are at least 18 and a registered citizen.

int age = 21; boolean isRegistered = true; boolean isCitizen = true; if (age >= 18 && isRegistered && isCitizen) { System.out.println("Eligible to vote"); }

Example 3: Mixed Operators

Problem: A student qualifies for the honor roll if their GPA is at least 3.5 or they have perfect attendance and a GPA of at least 3.0.

double gpa = 3.2; boolean perfectAttendance = true; // && binds tighter than || if (gpa >= 3.5 || (perfectAttendance && gpa >= 3.0)) { System.out.println("Honor roll!"); } // gpa=3.2: first part is false // perfectAttendance && 3.2 >= 3.0 = true && true = true // false || true = true ✓ Qualifies!

Using \(!\) with Compound Conditions

The NOT operator can negate an entire compound expression. De Morgan's Laws describe how negation distributes:

// De Morgan's Laws in Java: !(A && B) == (!A || !B) !(A || B) == (!A && !B) // Practical example: if (!(age >= 18 && isCitizen)) { ... } // Equivalent to: if (age < 18 || !isCitizen) { ... }

Common Mistakes

✕ Incomplete Expressions
Writing \(x > 5 \text{ && } < 10\) instead of \(x > 5 \text{ && } x < 10\). Both sides of \(\&\&\) must be complete boolean expressions.
✕ Using \(&\) Instead of \(\&\&\)
A single \(&\) is a bitwise operator, not a logical operator. Always use \(\&\&\) for boolean logic to get short-circuit evaluation.
✕ Forgetting Parentheses
Relying on precedence instead of parentheses: \(a \text{ || } b \text{ && } c\) means \(a \text{ || } (b \text{ && } c)\), not \((a \text{ || } b) \text{ && } c\). Always group for clarity.

Summary Reference

Operator Symbol true When Precedence
NOT \(!\) Operand is false 1 (highest)
AND \(\&\&\) Both operands true 2
OR \(||\) At least one true 3 (lowest)

Question 1 of 5

What does the following expression evaluate to?
\(\text{true && false || true}\)

Question 2 of 5

Which expression correctly checks if \(x\) is between 1 and 10, inclusive?

Question 3 of 5

What is the value of \(!(\text{true && false})\)?

Question 4 of 5

Given int x = 5;, what does this print?
System.out.println(x > 3 || x / 0 > 1);

Question 5 of 5

By De Morgan's Law, which expression is equivalent to \(!(a \text{ && } b)\)?