impera Open Impera

AP Computer Science A · Selection and iteration

Boolean expressions

1Relational operators

A boolean expression evaluates to true or false. Relational operators compare values:

== (equal), != (not equal), <, >, <=, >=

int age = 17;
boolean adult = age >= 18;   // false
  • Use == and != for primitives. For objects, == compares references, so use equals for content.
  • Do not confuse = (assignment) with == (comparison).
  • Avoid == with double results of calculations because of round-off: test Math.abs(a - b) < 0.0001 instead.
Check yourself

What is the value of 7 != 3 + 4?

  • 7

  • An error

  • false

  • true

Show the answer

false

3 + 4 is 7, which equals 7.

2Logical operators

  • && (AND): true only if both sides are true.
  • || (OR): true if at least one side is true.
  • ! (NOT): reverses the value.

Precedence: ! first, then &&, then ||. Use parentheses to be clear.

Worked example

int x = 5;

  1. x > 0 && x < 10 → true && true → true.
  2. x < 0 || x % 5 == 0 → false || true → true.
  3. !(x == 5) → false.

&& needs both true; || needs at least one true

Check yourself

For a = true, b = false, what is !a || b?

  • null

  • true

  • false

  • Error

Show the answer

false

false || false.

3Short-circuit evaluation

Java evaluates && and || left to right and stops as soon as the result is known:

  • false && ... is false without checking the right side.
  • true || ... is true without checking the right side.

This is used to avoid errors:

if (count != 0 && total / count > 5) { ... }
if (s != null && s.length() > 0) { ... }

If count is 0, the division is never evaluated, so no ArithmeticException.

Check yourself

In (x != 0 && 10 / x > 2) with x = 0, what happens?

  • The expression is true because x != 0 is evaluated last

  • An ArithmeticException is thrown when 10 / x is evaluated

  • The expression is false and no exception occurs

  • A compile-time error occurs because x could be zero

Show the answer

The expression is false and no exception occurs

Short-circuit stops after false.

4De Morgan's laws and equivalence

De Morgan's laws rewrite negated expressions:

$$!(a \;\&\&\; b) \equiv !a \;||\; !b \qquad !(a \;||\; b) \equiv !a \;\&\&\; !b$$

Worked example

simplify !(x > 3 && y <= 5).

  1. Apply De Morgan: !(x > 3) || !(y <= 5).
  2. Negate each comparison: x <= 3 || y > 5.

Two boolean expressions are equivalent if they give the same value for every input; check with a truth table.

Check yourself

Which is equivalent to !(a || b)?

  • !a && !b

  • !a || b

  • !a || !b

  • a && b

Show the answer

!a && !b

De Morgan's law.

5Going deeper: De Morgan's laws, short-circuiting and proving equivalence

Relational operators produce booleans: ==, !=, less than, greater than, and the or-equal forms. Logical operators combine them:

  • !a (not): true when a is false.
  • a && b (and): true only when both are true.
  • a || b (or): true when at least one is true.

Precedence: ! first, then &&, then ||. So a || b && c means a || (b && c). Use parentheses to be clear.

De Morgan's laws show how to distribute a !:

  • !(a && b) is equivalent to !a || !b.
  • !(a || b) is equivalent to !a && !b.

Negate each part and swap the operator.

Negating comparisons

  • !(x > y) is x <= y.
  • !(x >= y) is x < y.
  • !(x == y) is x != y.

Worked example (simplify): !(x > 5 && y <= 10).

  1. Negate each part: !(x > 5) is x <= 5; !(y <= 10) is y > 10.
  2. Swap && for ||.
  3. Result: x <= 5 || y > 10.

Worked example (simplify): !(a == b || c != d).

  1. a != b and c == d.
  2. Result: a != b && c == d.

Short-circuit evaluation: Java evaluates left to right and stops as soon as the answer is known.

  • In a && b, if a is false, b is not evaluated.
  • In a || b, if a is true, b is not evaluated.

This lets a first condition guard a second:

if (count != 0 && total / count > 50) { ... }
if (str != null && str.length() > 3) { ... }
if (i < arr.length && arr[i] == target) { ... }

Reverse the order and each would crash in the bad case (division by zero, NullPointerException, ArrayIndexOutOfBoundsException).

Worked example

with x = 0, what happens in if (x != 0 || 10 / x > 1)?

  1. x != 0 is false.
  2. With ||, a false first operand means the second must be evaluated.
  3. 10 / x throws an ArithmeticException. The guard needed &&, not ||.

Proving two expressions equivalent: build a truth table with every combination of inputs. They are equivalent only if the result columns match in every row.

Worked example

is !(a && b) the same as !a && !b?

  1. Try a = true, b = false.
  2. !(a && b) is !(false) = true.
  3. !a && !b is false && true = false.
  4. One differing row is enough: not equivalent. (De Morgan requires ||.)

Useful identities

  • a && true is a; a || false is a.
  • a && false is false; a || true is true.
  • a && !a is always false; a || !a is always true.
  • (a && b) || (a && !b) simplifies to a.

Booleans are values: a comparison can be returned or stored directly.

return score >= 60;            // better than if/else returning true and false
boolean inRange = low <= n && n <= high;

A range test needs two comparisons joined by &&; low <= n <= high does not compile.

Comparing objects

  • == on references asks whether they are the same object (aliases).
  • .equals asks whether they are equal in content (as the class defines it).
  • Compare with null using ==: if (obj == null).

Common mistakes

  • Forgetting to swap && and || when applying De Morgan.
  • Negating > as < (it is <=).
  • Putting the guard condition second.
  • Writing x == true (just write x) or x = true (assignment).
  • Writing if (x > 0 && < 10): each side must be a complete comparison.
Check yourself

Which expression is equivalent to !(p || !q)?

  • !p && q

  • p && !q

  • !p || q

  • !p && !q

Show the answer

!p && q

Negate each part and change || to &&; !(!q) is q.

6For top marks

  • When negating a comparison, flip the operator fully: the opposite of < is >=, not >.
  • Order conditions so that short-circuiting protects against null references, division by zero and out-of-range indices.
  • Use truth tables to check equivalence questions on the multiple-choice section.
  • Compare strings with equals, never ==.
Example

!(score >= 50 || retake) simplifies to score < 50 && !retake.

Check yourself

What is the negation of x < 10?

  • x >= 10

  • x != 10

  • x <= 10

  • x > 10

Show the answer

x >= 10

Includes 10.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera