Compound
Boolean Conditions
Learn to combine multiple boolean expressions using logical operators in C++ to write powerful, precise conditional logic.
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.
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?
The Three Logical Operators
Truth Table: && (AND)
&& returns true only when both operands are true. If either is false, the result is false.
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
Truth Table: || (OR)
|| returns true when at least one operand is true. It is only false when both are false.
| A | B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
! (NOT) and Precedence
The ! operator inverts a single boolean value. Precedence determines the order operators are evaluated when no parentheses are present.
&& vs ||: Key Distinction
| && (AND) | || (OR) | |
|---|---|---|
| Meaning | Both must be true | At least one must be true |
| Fails when | Either is false | Both are false |
| Use for | Narrowing conditions | Broadening conditions |
| Example | age >= 13 && age <= 19 | day == "Sat" || day == "Sun" |
| Precedence | Higher (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.
How to Write Compound Conditions
Example 1: Range Check
Check if a score is between 0 and 100 (inclusive). Both boundaries must hold simultaneously, so we use &&.
Example 2: Weekend Check
Check if a day is a weekend. Saturday or Sunday qualifies: either match is enough, so we use ||.
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.
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.
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.
Common Mistakes
x == 5 || 6 instead of x == 5 || x == 6. The literal 6 is always truthy, so the condition is almost always true.if (x = 5) (assignment) instead of if (x == 5) (comparison). The assignment returns 5, which is truthy, causing a logical bug.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 true | Left 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?
int x = 0;
if (x != 0 && 10 / x > 2) {
cout << "Yes";
} else {
cout << "No";
}