Lesson 17 of 60 – Logical Operators in C++
28%

Logical Operators in C++

Logical operators are used to combine two or more conditions. They are commonly used with if statements and loops when a program needs to check multiple conditions.

Note: C++ provides three main logical operators: && (AND), || (OR), and ! (NOT).

1. What are Logical Operators?

Logical operators combine or reverse Boolean conditions and produce a Boolean result: true or false.

int age = 25;

bool result = (age >= 18 && age <= 60);

Both conditions are true, so the final result is true.

2. Logical Operators in C++

Operator Name Purpose
&& Logical AND True when both conditions are true
|| Logical OR True when at least one condition is true
! Logical NOT Reverses a Boolean result

3. Logical AND Operator &&

The && operator is called the logical AND operator. It returns true only when both conditions are true.

int age = 25;

if (age >= 18 && age <= 60) {
    std::cout << "Age is within the range";
}

4. AND When Both Conditions are True

int a = 10;
int b = 20;

bool result = (a < 20 && b > 10);

The first condition is true and the second condition is also true. Therefore, the final result is true.

5. AND When One Condition is False

int a = 10;
int b = 20;

bool result = (a > 20 && b > 10);

The first condition is false. Therefore, the complete AND expression is false.

6. AND Truth Table

Condition A Condition B A && B
true true true
true false false
false true false
false false false

7. Logical OR Operator ||

The || operator is called the logical OR operator. It returns true when at least one condition is true.

int day = 6;

if (day == 6 || day == 7) {
    std::cout << "Weekend";
}

8. OR When Both Conditions are True

int a = 10;
int b = 20;

bool result = (a < 20 || b > 10);

Both conditions are true, so the OR expression is also true.

9. OR When One Condition is True

int age = 15;

bool result = (age >= 18 || age > 10);

The first condition is false, but the second condition is true. Therefore, the final result is true.

10. OR When Both Conditions are False

int age = 10;

bool result = (age >= 18 || age < 5);

Both conditions are false, so the OR expression is false.

11. OR Truth Table

Condition A Condition B A || B
true true true
true false true
false true true
false false false

12. Logical NOT Operator !

The ! operator is called the logical NOT operator. It reverses a Boolean result.

bool isStudent = true;

bool result = !isStudent;

The value of result becomes false.

13. NOT Truth Table

Condition !Condition
true false
false true

14. Using NOT with if

bool loggedIn = false;

if (!loggedIn) {
    std::cout << "Please log in";
}

Because loggedIn is false, !loggedIn becomes true.

15. Combining AND and OR

Multiple logical operators can be used in the same expression.

int age = 25;
bool hasID = true;

if (age >= 18 && hasID) {
    std::cout << "Allowed";
}

Here, both conditions must be true.

16. Logical Operators with Marks

int marks = 75;

if (marks >= 40 && marks <= 100) {
    std::cout << "Valid marks";
}

The AND operator checks that the marks are at least 40 and not more than 100.

17. Checking Multiple Conditions

int age = 22;
bool citizen = true;

if (age >= 18 && citizen) {
    std::cout << "Conditions satisfied";
}

This example requires both conditions to be true.

18. OR with Multiple Choices

char grade = 'A';

if (grade == 'A' || grade == 'B') {
    std::cout << "Good grade";
}

The condition is true if the grade is either A or B.

19. Logical Operators with User Input

#include <iostream>

int main() {

    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    if (age >= 18 && age <= 60) {
        std::cout << "Age is within the range";
    } else {
        std::cout << "Age is outside the range";
    }

    return 0;
}

20. Using Parentheses

Parentheses can make a complex logical expression easier to read and understand.

int age = 25;
bool student = true;

if ((age < 30) && student) {
    std::cout << "Condition matched";
}

21. Logical Operator Precedence

When several logical operators are used, their precedence affects how an expression is evaluated.

The logical NOT operator ! has higher precedence than logical AND &&, and logical AND has higher precedence than logical OR ||.

if (!a && b || c) {
    // condition
}

Using parentheses is recommended when an expression is complex.

22. Short-Circuit Evaluation

C++ uses short-circuit evaluation for built-in logical AND and OR operators.

For &&, if the left condition is false, the right condition does not need to be evaluated.

if (age > 0 && age < 100) {
    std::cout << "Valid range";
}

23. Short-Circuit OR

For ||, if the left condition is already true, the right condition does not need to be evaluated.

if (isAdmin || isTeacher) {
    std::cout << "Access allowed";
}

If isAdmin is true, the OR expression is already true.

24. Logical Operators and bool Variables

Logical operators can work directly with Boolean variables.

bool a = true;
bool b = false;

bool result1 = a && b;
bool result2 = a || b;
bool result3 = !a;

The results are:

  • a && b → false
  • a || b → true
  • !a → false

25. Common Mistake with Logical Operators

A common mistake is writing an incomplete comparison when checking a value against multiple choices.

Incorrect:

if (age == 18 || 20) {
    // ...
}

Correct:

if (age == 18 || age == 20) {
    // ...
}

Each value should be compared explicitly.

26. Logical Operators in a Login Example

#include <iostream>
#include <string>

int main() {

    std::string username;
    std::string password;

    std::cout << "Username: ";
    std::cin >> username;

    std::cout << "Password: ";
    std::cin >> password;

    if (username == "admin" && password == "1234") {
        std::cout << "Login successful";
    } else {
        std::cout << "Invalid login";
    }

    return 0;
}

The AND operator requires both conditions to be true.

27. Logical Operators in a Practical Example

#include <iostream>

int main() {

    int marks;
    int attendance;

    std::cout << "Enter marks: ";
    std::cin >> marks;

    std::cout << "Enter attendance: ";
    std::cin >> attendance;

    if (marks >= 40 && attendance >= 75) {
        std::cout << "Eligible";
    } else {
        std::cout << "Not eligible";
    }

    return 0;
}

28. Complete Logical Operators Example

#include <iostream>

int main() {

    int age = 25;
    bool hasID = true;
    bool hasPermission = false;

    if (age >= 18 && hasID) {
        std::cout << "Basic requirements satisfied"
                  << std::endl;
    }

    if (hasID || hasPermission) {
        std::cout << "At least one condition is true"
                  << std::endl;
    }

    if (!hasPermission) {
        std::cout << "Permission is not available"
                  << std::endl;
    }

    return 0;
}

29. Common Logical Operator Mistakes

  • Confusing && with ||.
  • Forgetting that AND requires both conditions to be true.
  • Forgetting that OR requires at least one condition to be true.
  • Using ! without understanding that it reverses a Boolean result.
  • Writing incomplete comparisons such as age == 18 || 20.
  • Using complex expressions without clear parentheses.
  • Confusing && with the bitwise AND operator &.
  • Confusing || with the bitwise OR operator |.

30. Logical Operators – Final Summary

Operator Name Result Example
&& Logical AND true when both conditions are true a > 0 && b > 0
|| Logical OR true when at least one condition is true a > 0 || b > 0
! Logical NOT reverses a Boolean result !isReady

📌 Key Points

  • C++ has three main logical operators: &&, ||, and !.
  • && is used when all required conditions must be true.
  • || is used when at least one condition must be true.
  • ! reverses a Boolean result.
  • Logical operators are commonly used with if statements and loops.
  • Use parentheses to make complex conditions easier to understand.
  • C++ uses short-circuit evaluation for built-in && and || operators.
  • Each comparison should be written explicitly when using multiple choices.
  • Do not confuse logical operators with bitwise operators.

🧠 Quick Quiz

Question: Which logical operator returns true only when both conditions are true?