01 / 20
&& ||

Compound
Boolean Expressions

Master how to combine multiple conditions with logical operators to build powerful decision-making logic in Java.

AP Computer Science A

What Is a Boolean Expression?

A Boolean expression is any expression that evaluates to exactly one of two values: true or false. In Java, the primitive type boolean stores this result.

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

Why Compound Expressions?

Real programs rarely depend on a single condition. You almost always need to check multiple things at once:

1
Login validation: Is the username correct AND is the password correct?
2
Eligibility check: Is the user age \(\ge 16\) OR do they have a waiver?
3
Input filtering: Is the value NOT negative?

The Three Logical Operators

&& (AND)
true only when both operands are true. If either is false, the result is false.
|| (OR)
true when at least one operand is true. Only false when both are false.
! (NOT)
Flips the value: true becomes false, and false becomes true.

&& (AND) Truth Table

A B
\(A \ \texttt{&&} \ B\)
true true true true false false false true false false false false

|| (OR) Truth Table

A B
\(A \ \texttt{||} \ B\)
true true true true false true false true true false false false

The ! (NOT) Operator

The NOT operator is unary: it takes a single Boolean operand and returns its opposite.

boolean isSunny = true; boolean isNotSunny = !isSunny; // false boolean isRaining = false; boolean isNotRaining = !isRaining; // true

&& vs ||: Key Distinctions

&& (AND) || (OR)
Result is true when Both are true At least one is true
Result is false when Either is false Both are false
Short-circuits when Left side is false Left side is true
Real-world analogy Both keys needed Any key works
Precedence Higher (evaluated first) Lower (evaluated second)

Short-Circuit Evaluation

Java stops evaluating a compound expression as soon as the result is determined. This is called short-circuit evaluation.

&& short-circuits
If the left side is false, the right side is never evaluated: the result must be false regardless.
|| short-circuits
If the left side is true, the right side is never evaluated: the result must be true regardless.
// Safe: short-circuit prevents NullPointerException if (str != null && str.length() > 0) { // str.length() only runs if str is not null }

How to Build Compound Expressions

1
Identify each condition. Write each simple Boolean expression separately (e.g., \(\texttt{age} \ge 18\), \(\texttt{hasID} == \texttt{true}\)).
2
Choose the operator. Do all conditions need to be true? Use &&. Does any one suffice? Use ||. Need to flip? Use !.
3
Add parentheses. Group sub-expressions to make precedence explicit and improve readability.
4
Test edge cases. Trace through with boundary values where conditions flip between true and false.

Example 1: Range Check

Check if a test score is between 0 and 100 inclusive.

int score = 85; boolean valid = (score >= 0) && (score <= 100); // true && true → true
0 100 85 valid range

Example 2: Eligibility Logic

A student qualifies for a discount if they are a member or if their purchase is over $50.

boolean isMember = false; double purchase = 72.50; boolean getsDiscount = isMember || (purchase > 50.0); // false || true → true

Example 3: Combined Logic

A ride requires age \(\ge 12\) and height \(\ge 48\) inches, or the rider has a special pass.

int age = 10; int height = 50; boolean hasPass = false; boolean canRide = (age >= 12 && height >= 48) || hasPass; // (false && true) || false // false || false → false

De Morgan's Laws

De Morgan's Laws let you distribute the NOT operator across compound expressions by flipping the operator and negating each part.

Law 1
\(!(\text{A} \ \texttt{&&} \ \text{B})\) is equivalent to \(!\text{A} \ \texttt{||} \ !\text{B}\)
Law 2
\(!(\text{A} \ \texttt{||} \ \text{B})\) is equivalent to \(!\text{A} \ \texttt{&&} \ !\text{B}\)
// These two are equivalent: !(x > 5 && y < 10) x <= 5 || y >= 10 // These two are equivalent: !(x == 0 || y == 0) x != 0 && y != 0

Common Mistakes

✕ Using = instead of ==
Writing \(\texttt{if (x = 5)}\) assigns 5 to x instead of comparing. Java will catch this as a type error for booleans, but be vigilant.
✕ Forgetting short-circuit
Placing a method call on the right side of && and assuming it always runs. If the left is false, the right is skipped.
✕ Incorrect range checks
Writing \(0 \le x \le 100\) does not work in Java. You must write \((\texttt{x} \ge 0) \ \texttt{&&} \ (\texttt{x} \le 100)\).

Summary Reference

Concept Syntax Behavior
AND && true only if both true
OR || true if at least one true
NOT ! Flips true/false
Precedence \(! > \texttt{&&} > \texttt{||}\) Use ( ) to clarify
De Morgan's 1 \(!(\text{A} \ \texttt{&&} \ \text{B})\) \(!\text{A} \ \texttt{||} \ !\text{B}\)
De Morgan's 2 \(!(\text{A} \ \texttt{||} \ \text{B})\) \(!\text{A} \ \texttt{&&} \ !\text{B}\)
Short-circuit && / || Stops early if result known

Question 1 of 5

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

Question 2 of 5

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

Question 3 of 5

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

Question 4 of 5

Given \(\texttt{String s = null;}\), what happens when this runs?
\(\texttt{if (s != null && s.length() > 0)}\)

Question 5 of 5

What is the value of result after this code runs?