01 / 21
&& ||

Logical Operators
& Short-Circuit

Master &&, ||, and ! in C++: how they evaluate, when they stop early, and why that matters for writing safe, efficient code.

C++

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.

&& AND
True only when both operands are true.
|| OR
True when at least one operand is true.
! NOT
Flips a single boolean value.

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.

// Is a student eligible for honors? if (gpa >= 3.5 && credits >= 60 && !onProbation) { cout << "Eligible!"; }

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.

&& Short-Circuit
If the left operand is false, the right operand is never evaluated: the whole expression must be false.
|| Short-Circuit
If the left operand is true, the right operand is never evaluated: the whole expression must be true.

Visualizing Short-Circuit Flow

A && B Evaluate A A? false F true Evaluate B = B A || B Evaluate A A? true T false Evaluate B = B SKIP B! SKIP B!

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:

1
! (NOT): evaluated first, like a negative sign.
2
&& (AND): evaluated next, like multiplication.
3
|| (OR): evaluated last, like addition.

How to Apply Logical Operators

1
Identify each sub-condition: break the compound condition into individual boolean checks.
2
Choose the connector: do you need ALL conditions true (&&) or ANY condition true (||)?
3
Order for safety: put the guard condition first so short-circuit prevents unsafe evaluation on the right.
4
Add parentheses: group sub-expressions to make precedence explicit and readable.
5
Trace with sample values: plug in true/false for each operand and verify the result.

Example 1: Safe Guard with &&

// Avoid division by zero using short-circuit int x = 0; int y = 10; if (x != 0 && y / x > 2) { cout << "Safe division"; }

Example 2: Pointer Guard Pattern

// Check pointer before dereferencing int* ptr = nullptr; if (ptr != nullptr && *ptr > 100) { cout << "Large value"; } // Safe! *ptr is never accessed when ptr is null.

Example 3: Combining && and ||

// Can a student register for the course? bool hasPassed = true; bool isHonors = false; bool hasWaiver = true; if ((hasPassed && isHonors) || hasWaiver) { cout << "Registered!"; }

Trace through the evaluation:

1
\(\text{hasPassed} \land \text{isHonors}\): \(\text{true} \land \text{false} = \)false
2
\(\text{false} \lor \text{hasWaiver}\): \(\text{false} \lor \text{true} = \)true (registered!)

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.

int count = 0; bool done = true; if (done || ++count > 5) { cout << count; // prints 0, NOT 1! }

De Morgan's Laws

When you negate a compound expression, the operator flips and each operand is negated:

\(\neg(A \land B)\)
is equivalent to
\(\neg A \lor \neg B\)
\(\neg(A \lor B)\)
is equivalent to
\(\neg A \land \neg B\)
// These two conditions are identical: if (!(age >= 18 && hasID)) { /* denied */ } if (age < 18 || !hasID) { /* denied */ }

Common Mistakes

✕ Using & Instead of &&
Single & is bitwise, not logical. It evaluates both sides and can crash on null pointer guards.
✕ Side Effects in Right Operand
Putting ++i or function calls on the right side means they may not execute due to short-circuit.
✕ Forgetting Precedence
Writing a || b && c when you mean (a || b) && c. Always use parentheses for clarity.

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) { }

Question 2 of 5

What is the value of result after this code?
bool result = true || false && false;

Question 3 of 5

What does count equal after execution?
int count = 0; bool flag = false;
if (flag && ++count > 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)?