Logical Operators in C++

Logical operators in C++ are used to combine two or more conditions. These operators help the program decide whether a final result is true (1) or false (0).

These operators are mostly used in decision-making statements like if, else-if, while, and loops.

Types of Logical Operators

C++ provides the following logical operators:

Operator Name Meaning Example
&& Logical AND True only if both conditions are true (5 > 2) && (8 > 3) → true
|| Logical OR True if any one condition is true (4 > 9) || (10 > 3) → true
! Logical NOT Reverses the condition (true → false) !(5 == 5) → false

Explanation of Logical Operator

1. Logical AND (&&)

The Logical AND operator returns true only when both conditions are true. If any one condition is false, the result becomes false.

  • Used when multiple conditions must be true together.
  • Common in range checks (e.g., age >= 18 && age <= 60).

Examples:

(10 > 5) && (20 > 15)   → true
(10 > 5) && (20 < 15)   → false

2. Logical OR (||)

The Logical OR operator returns true if any one of the conditions is true. It becomes false only when both conditions are false.

  • Used when at least one condition is enough to make the result true.
  • Helpful in permission checks (e.g., admin || teacher).

Examples:

(5 > 10) || (20 > 15)   → true
(5 > 10) || (20 < 15)   → false

3. Logical NOT (!)

The Logical NOT operator reverses the condition. It converts true → false and false → true.

  • Used to check the opposite of a condition.
  • Helpful in validation (e.g., !loggedIn).

Examples:

!(5 == 5)   → false
!(5 > 10)   → true

Example: Using All Logical Operators

Let us use all logical operators in a C++ program:

#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 20;

    cout << "Result of (a > 5) && (b > 15) : " << ((a > 5) && (b > 15)) << endl;
    cout << "Result of (a > 15) || (b > 15) : " << ((a > 15) || (b > 15)) << endl;
    cout << "Result of !(a == b) : " << (!(a == b)) << endl;

    return 0;
}

Output:

Result of (a > 5) && (b > 15) : 1

Result of (a > 15) || (b > 15) : 1

Result of !(a == b) : 1

Explanation

  • (a > 5) && (b > 15) → 10 > 5 and 20 > 15 → both conditions are true → result = 1.
  • (a > 15) || (b > 15) → 10 > 15 is false but 20 > 15 is true → one condition is true → result = 1.
  • !(a == b) → (10 == 20) is false → NOT false becomes true → result = 1.

Using Logical Operators in if Statements

Logical operators allow us to check multiple conditions in a single if statement. They help the program take decisions based on more than one condition.

Checking Age Eligibility for a Job

// C++ Program to check job eligibility
#include <iostream>
using namespace std;

int main() {

    int age = 25;

    if (age >= 18 && age <= 30) {
        cout << "Eligible for the job";
    }
    else {
        cout << "Not eligible for the job";
    }

    return 0;
}

Output:

Eligible for the job

Explanation

  • The job requires age between 18 and 30.
  • Here age = 25.
  • Both conditions are true: 25 ≥ 18 and 25 ≤ 30.
  • So true && true = true, therefore the if block runs.

Summary:

  • Logical operators help combine multiple conditions.
  • They return either 1 (true) or 0 (false).
  • C++ provides three logical operators: AND, OR, NOT.
  • Mostly used in if-else conditions and loops.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar