Logical Operators
& Short-Circuit
Master &&, ||, and ! in C++: how they evaluate, when they stop early, and why that matters for writing safe, efficient code.
What Are Logical Operators?
Logical operators let you combine or negate boolean expressions. They take operands that evaluate to true or false, and produce a single boolean result.
Truth Tables at a Glance
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
Why Logical Operators Matter
Nearly every program you write uses conditional logic. Logical operators are the glue that lets you express compound conditions: checking ranges, validating input, guarding against errors.
The Short-Circuit Rule
C++ evaluates logical expressions left to right and stops as soon as the result is determined. This is called short-circuit evaluation.
Visualizing Short-Circuit Flow
Logical vs Bitwise Operators
| Feature | Logical (&& || !) | Bitwise (& | ~) |
|---|---|---|
| Operand type | Boolean / truth values | Integer bits |
| Result type | bool (true/false) | int (bit pattern) |
| Short-circuits? | Yes | No |
| Common use | if / while conditions | Flags, masks, hardware |
Operator Precedence
Logical operators have different priority levels. From highest to lowest:
How to Apply Logical Operators
Example 1: Safe Guard with &&
Example 2: Pointer Guard Pattern
Example 3: Combining && and ||
Trace through the evaluation:
Side Effects & Short-Circuit
When the right operand has a side effect (like incrementing a variable or calling a function), short-circuiting means that side effect might not happen.
De Morgan's Laws
When you negate a compound expression, the operator flips and each operand is negated:
\(\neg A \lor \neg B\)
\(\neg A \land \neg B\)
Common Mistakes
Summary Reference
| Operator | Symbol | Short-Circuits When | Result |
|---|---|---|---|
| AND | && | Left is false | false |
| OR | || | Left is true | true |
| NOT | ! | N/A (unary) | Flips value |
Question 1 of 5
Given int x = 0;, what happens when this code runs?if (x != 0 && 10 / x > 2) { }
x != 0 is false, && short-circuits and 10 / x is never evaluated. No crash.Question 2 of 5
What is the value of result after this code?bool result = true || false && false;
&& binds tighter than ||. So it's true || (false && false) = true || false = true. Also, || short-circuits because the left side is already true.Question 3 of 5
What does count equal after execution?int count = 0; bool flag = false;if (flag && ++count > 0) { }
flag is false, && short-circuits. The expression ++count is never evaluated, so count stays at 0.Question 4 of 5
Which expression is equivalent to \(\neg(a \land b)\) by De Morgan's Law?
Question 5 of 5
Why is it dangerous to use & (single ampersand) instead of && in a null-pointer guard like if (ptr != nullptr & *ptr > 0)?
& operator does not short-circuit. Both operands are always evaluated, so *ptr is dereferenced even when ptr is null, causing undefined behavior (likely a crash).