01 / 22
\(&\&\) \(||\)

Compound
Boolean Conditions

Learn to combine multiple boolean expressions using logical operators in C++ to write powerful, precise conditional logic.

C++

What Is a Boolean Expression?

A boolean expression is any expression that evaluates to exactly one of two values: true or false. In C++, the type bool stores these values.

true
The condition is satisfied. Internally stored as the integer 1.
false
The condition is not satisfied. Internally stored as the integer 0.

Why Compound Conditions?

Real programs rarely depend on a single test. You almost always need to check multiple conditions at once: is the user old enough and do they have a valid ID? Is the temperature too high or too low?

// A single condition is limiting: if (age >= 18) { ... } // A compound condition is precise: if (age >= 18 && hasID) { ... }

The Three Logical Operators

&& (AND)
Both sides must be true for the whole expression to be true.
|| (OR)
At least one side must be true for the whole expression to be true.
! (NOT)
Flips a single boolean: true becomes false, false becomes true.

Truth Table: && (AND)

&& returns true only when both operands are true. If either is false, the result is false.

A B A && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Truth Table: || (OR)

|| returns true when at least one operand is true. It is only false when both are false.

A B A || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

! (NOT) and Precedence

The ! operator inverts a single boolean value. Precedence determines the order operators are evaluated when no parentheses are present.

1
! (NOT) is evaluated first: highest precedence among logical operators.
2
&& (AND) is evaluated second: binds tighter than OR.
3
|| (OR) is evaluated last: lowest precedence among the three.

&& vs ||: Key Distinction

&& (AND) || (OR)
MeaningBoth must be trueAt least one must be true
Fails whenEither is falseBoth are false
Use forNarrowing conditionsBroadening conditions
Exampleage >= 13 && age <= 19day == "Sat" || day == "Sun"
PrecedenceHigher (binds tighter)Lower (binds looser)

Short-Circuit Evaluation

C++ evaluates compound conditions left to right and stops early when the result is already determined. This is called short-circuit evaluation.

&& short-circuits on false
If the left side is false, the right side is never evaluated: the whole AND must already be false.
|| short-circuits on true
If the left side is true, the right side is never evaluated: the whole OR must already be true.
// Safe: if ptr is null, *ptr is never accessed if (ptr != nullptr && *ptr > 0) { ... }

How to Write Compound Conditions

1
Identify each simple condition you need. Write each one as a complete boolean expression (e.g., \(x > 0\)).
2
Decide the relationship. Do all conditions need to be true (&&), or is any one enough (||)?
3
Combine with the correct operator. Each sub-expression must be a complete comparison, not a shorthand.
4
Add parentheses when mixing && and || to make precedence explicit and clear.
5
Test edge cases mentally: what happens when all conditions are true? All false? Mixed?

Example 1: Range Check

Check if a score is between 0 and 100 (inclusive). Both boundaries must hold simultaneously, so we use &&.

int score = 85; if (score >= 0 && score <= 100) { cout << "Valid score"; } else { cout << "Invalid score"; }
0 100 score = 85 Valid range

Example 2: Weekend Check

Check if a day is a weekend. Saturday or Sunday qualifies: either match is enough, so we use ||.

string day = "Saturday"; if (day == "Saturday" || day == "Sunday") { cout << "It's the weekend!"; } else { cout << "It's a weekday."; }

Example 3: Mixing Operators

A theme park ride requires: age \(\geq 12\) and (height \(\geq 140\) cm or accompanied by an adult). Parentheses control the grouping.

int age = 10; int height = 150; bool withAdult = true; if (age >= 12 && (height >= 140 || withAdult)) { cout << "Ride allowed"; } else { cout << "Ride denied"; }

Example 4: Using ! (NOT)

Sometimes it is clearer to negate a condition. Check if a user is not logged in or not verified before blocking access.

bool loggedIn = true; bool verified = false; if (!loggedIn || !verified) { cout << "Access denied"; } else { cout << "Welcome!"; }

De Morgan's Laws

When you negate a compound condition, the operators flip and each part gets negated. These equivalences are essential for simplifying logic.

Law 1
\(! (A \land B) \equiv !A \lor !B\)
Law 2
\(! (A \lor B) \equiv !A \land !B\)
// These two are equivalent: if (!(x > 0 && x < 10)) { ... } if (x <= 0 || x >= 10) { ... }

Common Mistakes

✕ Incomplete Comparisons
Writing x == 5 || 6 instead of x == 5 || x == 6. The literal 6 is always truthy, so the condition is almost always true.
✕ Using = Instead of ==
Writing if (x = 5) (assignment) instead of if (x == 5) (comparison). The assignment returns 5, which is truthy, causing a logical bug.
✕ Ignoring Precedence
Writing a || b && c expecting (a || b) && c, but it actually means a || (b && c). Always add parentheses.

Summary Reference

Concept Syntax Key Rule
AND&&Both sides must be true
OR||At least one side true
NOT!Flips true/false
Precedence! > && > ||Use parentheses to be safe
Short-circuit&& stops on false, || stops on trueLeft side evaluated first
De Morgan's\(! (A \land B) \equiv !A \lor !B\)Negate flips the operator

Question 1 of 5

What is the value of this expression when \(x = 7\)?
\(x > 5\) && \(x < 10\)

Question 2 of 5

Which code correctly checks if grade is the letter 'A' or 'B'?

Question 3 of 5

Given \(a = \text{true}\), \(b = \text{false}\), \(c = \text{true}\), what does this evaluate to?
a || b && c

Question 4 of 5

Which expression is equivalent to \(!(x > 3 \land y < 10)\) by De Morgan's Law?

Question 5 of 5

What is the output of this code?