C++ Logical Operators (original) (raw)

Last Updated : 31 Mar, 2026

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 ( && )

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++, logical operators return a boolean value (true or false). Internally, true is typically represented as 1 and false as 0.

Syntax

_expression1 ****&&** expression2

**Example

C++ `

#include using namespace std;

int main() { int age = 25; bool isStudent = true;

// Using AND operator in if condition
if (age > 18 && isStudent) {
    cout << "Student";
}
else {
    cout << "Not Student";
}
return 0;

}

`

**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 student. If both conditions are true, the message "Student" will be printed. Otherwise, the else statement is executed.

2. Logical OR 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:

Operand 1 Operand 2 Result
true true **true
true false **true
false true **true
false false **false

Syntax

_expression1 ****||** _expression2

**Example

C++ `

#include using namespace std;

int main() {

int n = 7;

// using logical or for conditional statement
if (n <= 0 || n >= 10) {
    cout << "n is not in range [0, 10]";
}
else {
    cout << "n is in range [0, 10]";
}

return 0;

}

`

Output

n is in range [0, 10]

**Explanation: In the above code, the condition **n <= 0 || n >= 10 checks whether the number is either less than equal to 0 **or greater than equal to 10. If either of these conditions is true, the message "The number is outside the range of 0 to 10." will be printed otherwise else statement is printed.

3. Logical NOT Operator ( ! )

The C++ **logical NOT operator ( ! ) is a unary operator that is used to negate the value of a condition. It returns true if the condition is false, and false if the condition is true. Here's the truth table for the NOT operator:

Operand 1 Result
true **false
false **true

Syntax

****!** _expression

**Example

C++ `

#include using namespace std;

int main() {

bool isLoggedIn = false;

// using logical not operator
if (!isLoggedIn) {
    cout << "Please log in.";
}
else {
    cout << "Welcome to GeeksforGeeks!";
}

return 0;

}

`

Try It Yourselfredirect icon

**Explanation: In the above code,the condition ****'!isLoggedIn'** checks whether the user is not logged in. If the condition is true (i.e., the user is not logged in), the message "Please log in." will be displayed otherwise else statement will be printed.