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.
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.
| 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 |
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";
}
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.
int a = 10;
int b = 20;
bool result = (a > 20 && b > 10);
The first condition is false. Therefore, the complete AND expression is false.
| Condition A | Condition B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
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";
}
int a = 10;
int b = 20;
bool result = (a < 20 || b > 10);
Both conditions are true, so the OR expression is also 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.
int age = 10;
bool result = (age >= 18 || age < 5);
Both conditions are false, so the OR expression is false.
| Condition A | Condition B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
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.
| Condition | !Condition |
|---|---|
| true | false |
| false | true |
bool loggedIn = false;
if (!loggedIn) {
std::cout << "Please log in";
}
Because loggedIn is false, !loggedIn becomes true.
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.
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.
int age = 22;
bool citizen = true;
if (age >= 18 && citizen) {
std::cout << "Conditions satisfied";
}
This example requires both conditions to be true.
char grade = 'A';
if (grade == 'A' || grade == 'B') {
std::cout << "Good grade";
}
The condition is true if the grade is either A or B.
#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;
}
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";
}
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.
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";
}
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.
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 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.
#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.
#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;
}
#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;
}
age == 18 || 20.| 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 |
Question: Which logical operator returns true only when both conditions are true?