Szerkesztő:LinguisticMystic/cpp/LogicalOp
🔍 Logical Operators in C++
[szerkesztés]Logical operators are an integral part of programming languages like C++, allowing programmers to evaluate multiple conditions, control decision-making, and dictate program flow. These operators return Boolean values (true or false) based on logical computations. In C++, the main logical operators are:
- AND (
&&) - OR (
||) - NOT (
!)
Understanding and mastering these operators will allow you to build more complex and flexible conditions in if, while, for, and other control statements.
✅ Understanding Logical Operators
[szerkesztés]
What are logical operators?
[szerkesztés]Logical operators operate on Boolean expressions (conditions that result in true or false) and return a Boolean result. You can use them to:
- Combine multiple conditions (
&&,||) - Invert a condition (
!) - Make decisions based on combinations of conditions
1️⃣ AND Operator (&&)
[szerkesztés]The AND operator returns true only if both operands are true.
Syntax:
[szerkesztés]condition1 && condition2
Example:
[szerkesztés]bool weatherIsRainy = true;
bool umbrellaAvailable = false;
if (weatherIsRainy && umbrellaAvailable) {
cout << "You can go outside with an umbrella.";
} else {
cout << "It's rainy, but you don't have an umbrella.";
}
🟢 Output: It's rainy, but you don't have an umbrella.
Explanation:
[szerkesztés]weatherIsRainyistrueumbrellaAvailableisfalsetrue && falseisfalse, so theelseblock executes
Truth Table – AND (&&):
[szerkesztés]| A (left) | B (right) | A && B |
|---|---|---|
| false | false | false |
| false | true | false |
| true | false | false |
| true | true | true |
2️⃣ OR Operator (||)
[szerkesztés]The OR operator returns true if at least one operand is true.
Syntax:
[szerkesztés]condition1 || condition2
Example:
[szerkesztés]bool hasRaincoat = true;
bool hasUmbrella = false;
if (hasRaincoat || hasUmbrella) {
cout << "You can handle the rain with a raincoat or umbrella.";
} else {
cout << "You might get wet since you don't have a raincoat or umbrella.";
}
🟢 Output: You can handle the rain with a raincoat or umbrella.
Truth Table – OR (||):
[szerkesztés]| A | B | A | B |
|---|---|---|---|
| false | false | false | |
| false | true | true | |
| true | false | true | |
| true | true | true |
3️⃣ NOT Operator (!)
[szerkesztés]The NOT operator is a unary operator that inverts the Boolean value.
Syntax:
[szerkesztés]!condition
Example:
[szerkesztés]bool rainyCondition = true;
if (!rainyCondition) {
cout << "It's not rainy, so you don't need an umbrella.";
} else {
cout << "It's rainy, so consider taking an umbrella.";
}
🟢 Output: It's rainy, so consider taking an umbrella.
Truth Table – NOT (!):
[szerkesztés]| A | !A |
|---|---|
| true | false |
| false | true |
🔗 Combining Logical Operators
[szerkesztés]You can combine logical operators using parentheses to form complex logical expressions.
Example:
[szerkesztés]bool weatherIsRainy = true;
bool umbrellaAvailable = true;
bool isItRaining = true;
if (isItRaining && !umbrellaAvailable) {
cout << "It's raining and you don't have an umbrella. Stay indoors.\n";
} else if ((weatherIsRainy || !isItRaining) && umbrellaAvailable) {
cout << "It's a rainy weather or not raining, and you have an umbrella. You can go outside.\n";
} else {
cout << "Conditions don't match any specific case.\n";
}
🟢 Output: It's a rainy weather or not raining, and you have an umbrella. You can go outside.
🧪 More Examples
[szerkesztés]
🛒 Discount Checker:
[szerkesztés]int orderTotal = 120;
bool isPrimeMember = true;
if (orderTotal >= 100 || isPrimeMember) {
cout << "You qualify for a discount!";
} else {
cout << "No discount available.";
}
🟢 Output: You qualify for a discount!
📚 Exam Study Reminder:
[szerkesztés]bool hasExamTomorrow = true;
bool isTired = false;
if (!hasExamTomorrow || isTired) {
cout << "Take a break and relax!\n";
} else {
cout << "Keep studying for your exam.\n";
}
🟢 Output: Keep studying for your exam.
📤 Output of Boolean Values
[szerkesztés]By default, bool values print as 1 (true) and 0 (false). You can change this using std::boolalpha.
Example:
[szerkesztés]#include <iostream>
using namespace std;
int main() {
bool a = true;
bool b = false;
bool result = a && b;
cout << "a && b: " << result << endl;
cout << boolalpha;
cout << "a && b (boolalpha): " << result << endl;
return 0;
}
🟢 Output:
a && b: 0 a && b (boolalpha): false
🧠 Conclusion
[szerkesztés]Logical operators are the building blocks for making decisions in C++. Whether you’re checking user input, evaluating system conditions, or creating game logic, logical operators help you:
- Combine multiple conditions (
&&,||) - Invert results (
!) - Write clean, readable control flow
Mastering logical operators is a key step toward becoming a skilled C++ developer.