1. Intro
Logical operators combine or invert boolean expressions to build compound conditions. C++ has three logical operators: && (logical AND), || (logical OR), and ! (logical NOT). They are essential for writing multi-condition if statements and loop guards.
Cricket analogy: Selecting a player requires 'fit AND available' (&&) — both conditions must hold — while a wildcard pick might accept 'in-form OR experienced' (||), and 'NOT injured' (!) flips a single fitness flag, exactly how C++ logical operators combine conditions.
2. Syntax
cond1 && cond2 // true only if both are true
cond1 || cond2 // true if at least one is true
!cond1 // true if cond1 is false, and vice versa3. Explanation
&& returns true only when both operands are true. || returns true when at least one operand is true. ! inverts a single boolean value. C++ logical operators use **short-circuit evaluation**: for &&, if the left operand is false, the right operand is never evaluated (the whole expression must be false). For ||, if the left operand is true, the right operand is never evaluated. This matters when the right operand has side effects, like a function call or a pointer dereference guarded by a null check.
Cricket analogy: In DRS, 'umpire's call AND review requested' (&&) only overturns if both hold, but if the first check already fails, the system never even evaluates the ball-tracking replay — short-circuiting, just like C++'s && skipping the right operand.
Short-circuit evaluation means the right-hand operand of && or || may never run. This is often used deliberately, e.g. if (ptr != nullptr && ptr->value > 0) — the dereference ptr->value only happens if ptr is non-null, because && short-circuits on a false left side. But it also means a right-side function call with side effects (like incrementing a counter) may silently be skipped.
4. Example
#include <iostream>
using namespace std;
bool sideEffect() {
cout << "sideEffect() called" << endl;
return true;
}
int main() {
int age = 25;
bool hasLicense = true;
cout << boolalpha;
cout << "Can drive: " << (age >= 18 && hasLicense) << endl;
bool a = false;
cout << "Short-circuit test: " << (a && sideEffect()) << endl;
return 0;
}5. Output
Can drive: true
Short-circuit test: false6. Key Takeaways
&&is true only when both operands are true;||is true if at least one is true;!inverts a boolean.- C++ uses short-circuit evaluation:
&&skips the right operand if the left is false;||skips it if the left is true. - Short-circuiting is commonly used to guard unsafe operations, e.g. null-pointer checks before dereferencing.
- Because
sideEffect()was never called in the example, its print statement never ran — proving short-circuit behavior.
Practice what you learned
1. In `if (false && sideEffect())`, is `sideEffect()` called?
2. In `if (true || sideEffect())`, is `sideEffect()` called?
3. What does `!true` evaluate to?
4. Why does `if (ptr != nullptr && ptr->value > 0)` avoid a crash when `ptr` is null?
5. What is the result of `false || false || true`?
Was this page helpful?
You May Also Like
Relational Operators in C++
Understand C++ relational (comparison) operators such as ==, !=, <, >, <=, and >=, and how they always evaluate to a bool result.
if-else Statement in C++
Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.
Ternary Operator in C++
Learn how the C++ ternary conditional operator (?:) provides a compact alternative to if-else for simple value selection.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics