Logical AND Operator ( && )

The C++ logical AND operator (&&) is a binary operator that returns true if both of its operands are true. Otherwise, it returns false. Here’s the truth table for the AND operator:

Operand 1

Operand 2

Result

true

true

true

true

false

false

false

true

false

false

false

false

Note: In C, false is represented by 0 while the true is represented as any non-zero value, generally 1.

Syntax of Logical AND

expression1  &&  expression2

Example of Logical AND in C++

C++




// C++ Program to illustrate the logical AND Operator
#include <iostream>
using namespace std;
  
int main()
{
  
    // initialize variables
    int age = 25;
    bool isStudent = true;
  
    // Using AND operator in if condition
    if (age > 18 && isStudent) {
        cout << "You are eligible for a student discount."
             << endl;
    }
    else {
        cout << "You are not eligible for a student "
                "discount."
             << endl;
  
        return 0;
    }


Output

You are eligible for a student discount.

Explanation: In the above code, we have used AND operator in the if condition to check whether the age is greater than 18 and the person is a check. If both conditions are true, the message “You are eligible for a student discount.” will be printed. Otherwise, the else statement is executed.

C++ Logical Operators

In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false).

In C++, there are three logical operators:

  1. Logical AND ( && ) Operator
  2. Logical OR ( || ) Operator
  3. Logical NOT ( ! ) Operator

Let’s discuss each of the operators in detail.

Similar Reads

1. Logical AND Operator ( && )

The C++ logical AND operator (&&) is a binary operator that returns true if both of its operands are true. Otherwise, it returns false. Here’s the truth table for the AND operator:...

2. Logical OR Operator ( || )

...

3. Logical NOT Operator ( ! )

The C++ logical OR operator ( || ) is a binary operator that returns true if at least one of its operands is true. It returns false only when both operands are false. Here’s the truth table for the OR operator:...

Conclusion

...

Contact Us