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 useequalsfor content. - Do not confuse
=(assignment) with==(comparison). - Avoid
==withdoubleresults of calculations because of round-off: testMath.abs(a - b) < 0.0001instead.
Check yourself
What is the value of 7 != 3 + 4?
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.
int x = 5;
x > 0 && x < 10→true && true→ true.x < 0 || x % 5 == 0→false || true→ true.!(x == 5)→ false.
&& needs both true; || needs at least one true
Check yourself
For a = true, b = false, what is !a || b?
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?
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$$
simplify !(x > 3 && y <= 5).
- Apply De Morgan:
!(x > 3) || !(y <= 5). - 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)?
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)isx <= y.!(x >= y)isx < y.!(x == y)isx != y.
Worked example (simplify): !(x > 5 && y <= 10).
- Negate each part:
!(x > 5)isx <= 5;!(y <= 10)isy > 10. - Swap
&&for||. - Result:
x <= 5 || y > 10.
Worked example (simplify): !(a == b || c != d).
a != bandc == d.- 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, ifais false,bis not evaluated. - In
a || b, ifais true,bis 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).
with x = 0, what happens in if (x != 0 || 10 / x > 1)?
x != 0is false.- With
||, a false first operand means the second must be evaluated. 10 / xthrows 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.
is !(a && b) the same as !a && !b?
- Try a = true, b = false.
!(a && b)is!(false)= true.!a && !bisfalse && true= false.- One differing row is enough: not equivalent. (De Morgan requires
||.)
Useful identities
a && trueisa;a || falseisa.a && falseis false;a || trueis true.a && !ais always false;a || !ais always true.(a && b) || (a && !b)simplifies toa.
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)..equalsasks 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 writex) orx = true(assignment). - Writing
if (x > 0 && < 10): each side must be a complete comparison.
Check yourself
Which expression is equivalent to !(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==.
!(score >= 50 || retake) simplifies to score < 50 && !retake.
Check yourself
What is the negation of 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