100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Logical Operators in C++

Master the C++ logical operators &&, ||, and !, and understand short-circuit evaluation and how it can skip side effects.

OperatorsBeginner6 min readJul 7, 2026
Analogies

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

cpp
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 versa

3. 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

cpp
#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

text
Can drive: true
Short-circuit test: false

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#LogicalOperatorsInC#Logical#Operators#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep