AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/2/2-5)
Unit 2 · Topic 2.5
2.5 Compound Boolean Expressions
Real conditions usually combine several facts: in range and not empty, a weekend or a holiday. This topic covers the logical operators !, && and ||, the order Java applies them in, and short-circuit evaluation, which can skip part of a condition.
Key terms
- logical operator
&&||!- short-circuit evaluation
- truth table
The three logical operators
Logical operators combine Boolean values into a new boolean.
!a(not): true whenais false, and false whenais true.a && b(and): true only when bothaandbare true.a || b(or): true when at least one of them is true, including when both are.
Truth tables
A truth table lists every possible combination of inputs and the result for each. With two Boolean inputs there are four rows. In the table below, the "and" column is a && b and the "or" column is a || b. Notice that "and" is true in only one row, while "or" is false in only one row.
| a | b | and | or |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
Order of operations
Java applies ! first, then &&, then ||. Relational operators like < and == come before all of the logical operators except !. So weekend || hour > 20 && raining is read as weekend || ((hour > 20) && raining).
The ! sticks only to what's right next to it. !raining && hour > 12 means (!raining) && (hour > 12). To negate a whole expression, use parentheses: !(raining && hour > 12).
Checking a range needs two comparisons: hour >= 9 && hour < 17 is true from 9 up to, but not including, 17.
Short-circuit evaluation
Java evaluates the left side first, and sometimes that's enough to know the answer. If the left side of && is false, the whole thing must be false, so the right side is never evaluated. If the left side of || is true, the whole thing must be true, so again the right side is skipped. This is short-circuit evaluation.
It's useful as a guard. Put the safety check on the left:
int total = 50;
int count = 0;
if (count != 0 && total / count > 10)
{
System.out.println("high average");
}
else
{
System.out.println("no data or low");
}
Since count != 0 is false, Java never does total / count, so there's no division by zero. It prints no data or low. Swap the two sides, and the division runs first and throws an ArithmeticException:
int total = 50;
int count = 0;
if (total / count > 10 && count != 0)
{
System.out.println("high average");
}
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Evaluating compound expressions
What does this code print?
int hour = 14; boolean weekend = false; boolean raining = true; boolean a = hour >= 9 && hour < 17; boolean b = weekend || raining; boolean c = !raining && hour > 12; boolean d = weekend || hour > 20 && raining; System.out.println(a + " " + b + " " + c + " " + d);Show the solutionHide the solution
- Step 1:
a:14 >= 9is true and14 < 17is true, sotrue && trueistrue. - Step 2:
b:false || trueistrue. - Step 3:
c:!applies only toraining, givingfalse. Thenfalse && (14 > 12)isfalse; the right side isn't even checked. - Step 4:
d:&&goes before||.hour > 20is false, sofalse && rainingisfalse. Thenweekend || falseisfalse || false, which isfalse.
Answer: It prints
true true false false. - Step 1:
- Example 2
Short-circuit as a guard
What happens when this code runs? Explain using short-circuit evaluation.
int total = 50; int count = 0; if (total / count > 10 && count != 0) { System.out.println("high average"); }Show the solutionHide the solution
- Step 1: Java evaluates the left side of
&&first:total / count, which is50 / 0. - Step 2: Integer division by zero throws an
ArithmeticExceptionright away, so the program stops. The right side,count != 0, never gets a chance to protect it. - Step 3: If the condition were written
count != 0 && total / count > 10, the left side would be false, Java would skip the division, and theifbody would simply not run.
Answer: It throws an
ArithmeticExceptionand prints nothing. Putcount != 0on the left so short-circuiting skips the division. - Step 1: Java evaluates the left side of
Common mistakes
- Forgetting that
&&comes before||. Add parentheses in your head before evaluating. - Writing
!a && bwhen you mean!(a && b). The!only applies to what's right after it. - Putting a guard like
count != 0on the right of&&. It must be on the left to protect the risky part. - Using
||when a value must meet both conditions, as in a range check.x > 0 || x < 10is true for every number.
On the exam
- Expect questions where the right side of
&&or||would throw an exception or change a variable. Decide first whether short-circuiting skips it. - In free-response code, guard method calls and divisions with a check on the left of
&&, such as checking an index is in range before using it.
Connected topics
Videos
Check yourself
4 questions on 2.5 Compound Boolean Expressions. Pick an answer to see if you got it, and why.
Consider the following code segment.boolean r = true || false && false;
boolean s = !true || true && false;
System.out.println(r + " " + s);What is printed as a result of executing the code segment?
Consider the following code segment.int count = 0;
int total = 10;
if (count != 0 && total / count > 2)
{
System.out.println("high");
}
else
{
System.out.println("safe");
}What is printed as a result of executing the code segment?
Consider the following method.public static boolean check(int n)
{
System.out.print(n + " ");
return n > 2;
}The following code segment appears in another method of the same class.if (check(1) && check(3))
{
System.out.print("A");
}
if (check(5) || check(0))
{
System.out.print("B");
}What is printed as a result of executing the code segment?
Which of the following expressions evaluates to true exactly when the int variable x is between 1 and 10, inclusive?
0 of 4 answered