Unit 2 · 18 min
2.5 Compound Boolean Expressions
Logical operators combine true and false values, and Java stops evaluating as soon as the answer is known.
Summary, objectives and course and exam description (CED) codes
The logical operators ! (not), && (and) and || (or) combine Boolean expressions into compound Boolean expressions that evaluate to a Boolean value. Java applies ! first, then &&, then ||. With short-circuit evaluation, when the first operand of && or || already decides the result, the second Boolean expression is not evaluated.
By the end of this session you can
- 2.5.A Develop code to represent compound Boolean expressions and determine the result of these expressions.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
1. The three logical operators
Logical operators are used with Boolean expressions 2.5.A.1. Each one produces a Boolean value.
!a(not) is true ifais false, and false otherwise.a && b(and) is true if bothaandbare true, and false otherwise.a || b(or) is true ifais true,bis true, or both are true, and false otherwise.
A truth table lists every combination of inputs:
| a | b | !a | a && b | a || b |
|---|---|---|---|---|
| true | true | false | true | true |
| true | false | false | false | true |
| false | true | true | false | true |
| false | false | true | false | false |
int age = 15;
boolean hasTicket = true;
System.out.println(age >= 13 && hasTicket);
System.out.println(age >= 18 || hasTicket);
System.out.println(!hasTicket);
Output:
true
true
false
An expression that uses logical operators evaluates to a Boolean value, so you can store it, print it or use it as an if condition 2.5.A.1.
2. Order of precedence
When an expression mixes logical operators, Java evaluates ! (not) first, then && (and), then || (or) 2.5.A.1. Arithmetic and relational operators are evaluated before && and ||.
boolean a = true;
boolean b = false;
boolean c = false;
System.out.println(a || b && c);
System.out.println((a || b) && c);
System.out.println(!a || c);
Output:
true
false
false
Step by step for a || b && c: first b && c is false && false, which is false. Then a || false is true || false, which is true. Parentheses change the order: (a || b) && c is true && false, which is false.
Watch out: ! binds very tightly. To negate a comparison, put the comparison in parentheses.
int x = 5;
boolean result = !x > 3;
error: bad operand type int for unary operator '!'
The fix is !(x > 3).
3. Ranges with && and ||
To check whether a value is inside a range, join two comparisons with &&. To check whether it is outside, use ||.
int hour = 22;
boolean openHours = hour >= 9 && hour <= 17;
boolean quietHours = hour < 7 || hour > 21;
System.out.println("Open: " + openHours);
System.out.println("Quiet: " + quietHours);
Output:
Open: false
Quiet: true
Watch out: hour < 7 && hour > 21 can never be true, because no number is both below 7 and above 21. This is a logic error: the code compiles, but the condition is always false.
4. Short-circuit evaluation
Short-circuit evaluation: when the first Boolean expression alone already settles the result of && or ||, Java does not evaluate the second Boolean expression at all 2.5.A.2.
false && anythingis always false, so Java skipsanything.true || anythingis always true, so Java skipsanything.
Programmers use this on purpose as a safety check. Here the division only happens when it is safe:
int games = 0;
int points = 40;
if (games != 0 && points / games > 10) {
System.out.println("High scorer");
} else {
System.out.println("Not enough data");
}
Output:
Not enough data
games != 0 is false, so points / games is never evaluated. Swap the two sides and the division runs first:
int games = 0;
int points = 40;
if (points / games > 10 && games != 0) {
System.out.println("High scorer");
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
The same idea protects a method call on a reference that might be null:
String nickname = null;
if (nickname != null && nickname.length() > 3) {
System.out.println("Long nickname");
} else {
System.out.println("No long nickname");
}
Output:
No long nickname
5. Short-circuit and side effects
If the second expression calls a method that does something visible, such as printing, short-circuit evaluation means that call may not happen. Exam questions use this to test whether you really know which side runs.
public class ShortCircuitDemo {
public static boolean isLong(String word) {
System.out.println("Checking " + word);
return word.length() > 5;
}
public static void main(String[] args) {
boolean first = 5 > 2 || isLong("banana");
System.out.println("first: " + first);
boolean second = 5 < 2 || isLong("kiwi");
System.out.println("second: " + second);
}
}
Output:
first: true
Checking kiwi
second: false
In first, the left side is true, so isLong("banana") is never called and nothing extra is printed. In second, the left side is false, so Java must evaluate the right side: isLong("kiwi") runs, prints its line and returns false.
Lock it in: memory hooks
- &&AND is strict: everyone must say yes.
- ||OR is easygoing: one yes is enough.
- PrecedenceNot, And, Or: N-A-O, in that order, like * before +.
- Short-circuitPut the safety check on the left: if it fails, the risky part never runs.
Common misconceptions
"|| means one or the other, but not both."
Not quite. a || b is true when a is true, b is true, or both are true. It is false only when both are false.
"Java evaluates && and || from left to right with equal priority."
Not quite. && has higher precedence than ||, just as * is done before +. true || false && false is true, because false && false is evaluated first.
"Java always evaluates both sides of && and ||."
Not quite. Short-circuit evaluation skips the second expression when the first already decides the result: false for && and true for ||.
Exam traps
- count != 0 && total / count > 5 is safe when count is 0. Swap the two sides and the division runs first, throwing an ArithmeticException.
- x < 0 && x > 10 is never true. To test outside a range, use ||: x < 0 || x > 10.
- true || false && false is true, not false: && is done before ||.
- !x > 3 does not compile when x is an int. Write !(x > 3).
- Short-circuit also skips anything the second expression would do, such as a method call that prints.
2PracticeRecall it, then check yourself
In 30 seconds
- !a is true when a is false, and false when a is true.
- a && b is true only when both are true. a || b is true when at least one is true.
- Precedence: ! first, then &&, then ||. Arithmetic and relational operators come before && and ||.
- A compound Boolean expression evaluates to a single Boolean value.
- Short-circuit: false && ... and true || ... stop early, so the second expression is not evaluated.
- Write ranges with two comparisons: x >= 1 && x <= 10.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 5 questions
Checkpoint: Compound Boolean Expressions
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
3PrevailLock it in for exam day
By the numbers
- Logical operators, in order of precedence three operators CED 2.5.A.1
- !not
- &&and
- ||or
- Cases where short-circuit evaluation skips the second expression two cases CED 2.5.A.2
- && when the first expression is falseresult is false
- || when the first expression is trueresult is true
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Logical operatorstap to flip
The operators ! (not), && (and) and || (or), which combine Boolean expressions into a compound Boolean expression. Precedence: !, then &&, then ||.
Example and CED codes -
Short-circuit evaluationtap to flip
When the first operand of && or || already decides the result, the second Boolean expression is not evaluated.
Example and CED codes -
Boolean expressiontap to flip
An expression that evaluates to a Boolean value, true or false, such as a comparison with a relational operator or a combination with logical operators.
Example and CED codes -
Truth tabletap to flip
A table that lists every combination of true and false for the variables in Boolean expressions, with the value of each expression in every row.
Example and CED codes -
Relational operatorstap to flip
The operators ==, !=, <, >, <= and >=, which compare two values and evaluate to a Boolean value.
Example and CED codes
Questions students ask
What are the logical operators in Java?
! (not), && (and) and || (or). They work on Boolean values and produce a Boolean value.
What is short-circuit evaluation in Java?
When the first operand of && is false, or the first operand of || is true, the result is already known, so Java does not evaluate the second operand.
Which is evaluated first, && or ||?
&&. The order of precedence is ! (not), then && (and), then || (or). Use parentheses when you want a different order.
How do I check if a number is between two values in Java?
Use two comparisons joined by &&, for example x >= 1 && x <= 10. Java does not allow 1 <= x <= 10.