dev only
Compound Boolean Expressions
And, Or, Not
Combine conditions with && (and), || (or), and ! (not):
if (armReady && intakeReady)
{
scoreCommand.schedule();
}
&& needs both sides true; || needs just one (or both). ! flips a boolean's value.
Short-Circuit Evaluation
Java stops checking as soon as the answer is already known — it never evaluates more than it needs to:
- In
a && b: ifais false,bis never even checked — the whole thing is already false. - In
a || b: ifais true,bis never checked — the whole thing is already true.
This isn't just an optimization — it's a real safety pattern:
if (sensor != null && sensor.isFaulty())
{
report(sensor);
}
If sensor is null, the left side is false, so && never evaluates sensor.isFaulty() — which would otherwise throw a NullPointerException. The order here matters: the null check has to come first.
Operator Precedence
! binds tightest, then &&, then || — parentheses override all of it and make intent explicit:
if (!ready || !armed) { ... } // ! applies to each individually
if (!(ready && armed)) { ... } // different! ! applies to the whole expression
Common Pitfalls
- Putting the null check on the wrong side.
sensor.isFaulty() && sensor != nullchecks the faulty status first — ifsensorisnull, it crashes before the null check ever runs. The null check must come first for short-circuiting to protect you. - Assuming
!(a && b)is the same as!a && !b. It's not — that's a real logic error (the correct equivalent, De Morgan's Law, comes in the next lesson).
Key Takeaways
&&requires both sides true;||requires at least one;!flips a boolean.- Short-circuit evaluation skips the right side once the left side already determines the result — order a null-check first specifically to exploit this.
!has higher precedence than&&, which has higher precedence than||; use parentheses when the default grouping isn't what you mean.
Derived from csawesome-2026: unit-2-selection-and-iteration/2.05-compound-boolean-expressions.md (CSA 2.5)