Logical operators in C++ are used to combine or invert boolean expressions. They play a critical role in control flow, allowing multiple conditions to be evaluated together.
Logical operators evaluate to a boolean value (true or false) and are commonly used in if statements, loops, and other conditional expressions.
List of Logical Operators in C++
Operator | Name | Description |
---|---|---|
&& | Logical AND | Returns true if both operands are true |
|| | Logical OR | Returns true if either operands are true |
! | Logical NOT | Inverts the boolean value of the operand |
1. Logical AND (&&)
The logical AND operator (&&) returns true only if both operands are true. If either operand is false, the entire expression evaluates to false.
#include <iostream> using namespace std; int main() { int age = 20; bool hasID = true; if (age >= 18 && hasID) { cout << "You are allowed entry." << endl; } else { cout << "Entry denied." << endl; } return 0; }
Explanation:
- The expression age >= 18 && hasID checks if age is 18 or older and hasID is true.
- If both conditions are true, “You are allowed entry” is printed.
Output:
You are allowed entry.
2. Logical OR (||)
The logical OR operator (||) returns true if at least one of the operands is true. If both operands are false, the expression evaluates to false.
#include <iostream> using namespace std; int main() { int temperature = 35; bool isRaining = true; if (temperature > 30 || isRaining) { cout << "Stay indoors." << endl; } else { cout << "You can go outside." << endl; } return 0; }
Explanation:
- The expression temperature > 30 || isRaining checks if the temperature is over 30 or if it’s raining.
- If either condition is true, “Stay indoors” is printed.
Output:
Stay indoors.
3. Logical NOT (!)
The logical NOT operator (!) inverts the boolean value of the operand. If the operand is true, it becomes false; if it’s false, it becomes true.
#include <iostream> using namespace std; int main() { bool isAvailable = false; if (!isAvailable) { cout << "Item is not available." << endl; } else { cout << "Item is available." << endl; } return 0; }
Explanation:
- !isAvailable inverts isAvailable. Since isAvailable is false, !isAvailable is true.
- “Item is not available” is printed if isAvailable is false.
Output:
Item is not available.
4. Combining Logical Operators
Logical operators can be combined to form more complex conditions. Use parentheses to clarify the order of evaluation.
#include <iostream> using namespace std; int main() { int age = 22; bool hasID = true; bool withParent = false; if ((age >= 18 && hasID) || withParent) { cout << "You are allowed entry." << endl; } else { cout << "Entry denied." << endl; } return 0; }
Explanation:
- The expression (age >= 18 && hasID) || withParent evaluates to true if the person is 18 or older and has an ID, or if they are with a parent.
- Parentheses group conditions, ensuring age >= 18 && hasID is evaluated together.
Output:
You are allowed entry.
5. Using Logical Operators in Conditional Statements
Logical operators are often used in if statements to make decisions based on multiple conditions.
#include <iostream> using namespace std; int main() { int score = 85; int attendance = 90; if (score >= 80 && attendance >= 75) { cout << "You passed the course." << endl; } else { cout << "You did not pass the course." << endl; } return 0; }
Explanation:
- score >= 80 && attendance >= 75 checks if both score and attendance meet the requirements for passing.
Output:
You passed the course.
6. Using Logical Operators in Loops
Logical operators can control loop execution based on multiple conditions.
#include <iostream> using namespace std; int main() { int count = 0; while (count < 10 && count % 2 == 0) { cout << "Count: " << count << endl; count += 2; } return 0; }
Explanation:
- The loop runs while count is less than 10 and even (count % 2 == 0).
- When count reaches 10, the loop stops.
Output:
Count: 0 Count: 2 Count: 4 Count: 6 Count: 8
7. Logical Operators with User Input
Logical operators can validate multiple conditions with user input.
#include <iostream> using namespace std; int main() { int age; char gender; cout << "Enter age: "; cin >> age; cout << "Enter gender (M/F): "; cin >> gender; if (age >= 18 && (gender == 'M' || gender == 'F')) { cout << "Eligible for the program." << endl; } else { cout << "Not eligible for the program." << endl; } return 0; }
Explanation:
- age >= 18 && (gender == ‘M’ || gender == ‘F’) checks if age is at least 18 and gender is either M or F.
Sample Output:
Enter age: 20 Enter gender (M/F): M Eligible for the program.
8. Short-Circuit Evaluation in Logical Operators
C++ uses short-circuit evaluation for && and ||. This means that if the result of the logical expression can be determined from the first operand, the second operand is not evaluated.
#include <iostream> using namespace std; bool checkAge(int age) { cout << "Checking age... "; return age >= 18; } bool checkID(bool hasID) { cout << "Checking ID... "; return hasID; } int main() { int age = 20; bool hasID = true; if (checkAge(age) && checkID(hasID)) { cout << "Access granted." << endl; } else { cout << "Access denied." << endl; } return 0; }
Explanation:
- If checkAge(age) returns false, checkID(hasID) is not called because the result of && would already be false.
Output:
Checking age... Access granted.
9. Logical NOT (!) with Relational Operators
The logical NOT operator (!) is often used to simplify conditions in if statements by inverting a relational expression.
#include <iostream> using namespace std; int main() { bool isStudent = true; bool isMember = false; if (!isStudent && !isMember) { cout << "You need to be a student or a member for access." << endl; } else { cout << "Access granted." << endl; } return 0; }
Explanation:
- !isStudent && !isMember checks if neither isStudent nor isMember is true.
Output:
Access granted.
10. Summary Table of Logical Operators in C++
Operator | Description | Example | Explanation |
---|---|---|---|
&& | Logical AND | a && b | Returns true if both a and b are true |
|| | Logical OR | `a || b | Returns true if a or b are true |
! | Logical NOT | !a | Returns true if a is false, inverts a |
Complete Example: Combining Logical Operators
This example demonstrates a complete program using logical operators to evaluate multiple conditions for entry eligibility based on age, parental supervision, and membership status.
#include <iostream> using namespace std; int main() { int age; bool isMember; bool withParent; cout << "Enter age: "; cin >> age; cout << "Are you a member? (1 for yes, 0 for no): "; cin >> isMember; cout << "Are you with a parent? (1 for yes, 0 for no): "; cin >> withParent; if ((age >= 18 || withParent) && isMember) { cout << "Entry granted." << endl; } else { cout << "Entry denied." << endl; } return 0; }
Explanation:
- The condition (age >= 18 || withParent) && isMember checks if the user is 18 or older or with a parent and is a member.
- If the condition is met, “Entry granted” is printed.
Sample Output:
Enter age: 16 Are you a member? (1 for yes, 0 for no): 1 Are you with a parent? (1 for yes, 0 for no): 1 Entry granted.
Logical operators in C++ enable complex condition handling and are essential for controlling program flow.