Skip to main content

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 when a is false, and false when a is true.
  • a && b (and): true only when both a and b are 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.

abandor
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

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.

  1. 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 solution
    1. Step 1: a: 14 >= 9 is true and 14 < 17 is true, so true && true is true.
    2. Step 2: b: false || true is true.
    3. Step 3: c: ! applies only to raining, giving false. Then false && (14 > 12) is false; the right side isn't even checked.
    4. Step 4: d: && goes before ||. hour > 20 is false, so false && raining is false. Then weekend || false is false || false, which is false.

    Answer: It prints true true false false.

  2. 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 solution
    1. Step 1: Java evaluates the left side of && first: total / count, which is 50 / 0.
    2. Step 2: Integer division by zero throws an ArithmeticException right away, so the program stops. The right side, count != 0, never gets a chance to protect it.
    3. Step 3: If the condition were written count != 0 && total / count > 10, the left side would be false, Java would skip the division, and the if body would simply not run.

    Answer: It throws an ArithmeticException and prints nothing. Put count != 0 on the left so short-circuiting skips the division.

Common mistakes

  • Forgetting that && comes before ||. Add parentheses in your head before evaluating.
  • Writing !a && b when you mean !(a && b). The ! only applies to what's right after it.
  • Putting a guard like count != 0 on 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 < 10 is 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

  • AP Computer Science A - Topic 2.5 - Part 1: Compound Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CS A 3.5 - Compound Boolean Expressions

    CodeHSWatch on YouTube (opens in a new tab)

  • AP CSA - Unit 3: If Statements - Lesson 4: Short Circuit Evaluation

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.5 - Part 2: Compound Boolean Expressions

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Java logical operators ❗ (AND OR NOT)

    Bro CodeWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 2.5 Compound Boolean Expressions. Pick an answer to see if you got it, and why.

Question 1 of 4

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?

Question 2 of 4

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?

Question 3 of 4

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?

Question 4 of 4

Which of the following expressions evaluates to true exactly when the int variable x is between 1 and 10, inclusive?

0 of 4 answered