|
| 1 | +--- |
| 2 | +Title: 'Logical Operators' |
| 3 | +Description: 'Logical operators perform logical operations such as AND, OR, and NOT to combine or modify boolean expressions in C++.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Logic' |
| 9 | + - 'Operators' |
| 10 | + - 'Boolean' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-c-plus-plus' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +**Logical operators** in C++ are used to perform logical operations on boolean expressions or values that can be evaluated as true or false. These [operators](https://www.codecademy.com/resources/docs/cpp/operators) are essential for creating complex conditional statements, controlling program flow, and implementing decision-making logic in applications. There are 3 logical operators in C++: |
| 17 | + |
| 18 | +- Logical AND (`&&`) |
| 19 | +- Logical OR (`||`) |
| 20 | +- Logical NOT (`!`) |
| 21 | + |
| 22 | +## Logical AND (`&&`) |
| 23 | + |
| 24 | +The logical AND operator `&&` returns true only when both operands are true. It uses short-circuit evaluation, meaning if the first operand is false, the second operand is not evaluated. |
| 25 | + |
| 26 | +### Syntax of `&&` |
| 27 | + |
| 28 | +```pseudo |
| 29 | +expression1 && expression2 |
| 30 | +``` |
| 31 | + |
| 32 | +Here: |
| 33 | + |
| 34 | +- `expression1` and `expression2` are boolean expressions or values that evaluate to true or false |
| 35 | + |
| 36 | +### Example of Logical AND (`&&`) Operator |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <iostream> |
| 40 | +using namespace std; |
| 41 | + |
| 42 | +int main() { |
| 43 | + int age = 25; |
| 44 | + bool hasLicense = true; |
| 45 | + |
| 46 | + // Check if person can drive |
| 47 | + if (age >= 18 && hasLicense) { |
| 48 | + cout << "Person can drive legally." << endl; |
| 49 | + } else { |
| 50 | + cout << "Person cannot drive legally." << endl; |
| 51 | + } |
| 52 | + |
| 53 | + return 0; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The output of this code is: |
| 58 | + |
| 59 | +```shell |
| 60 | +Person can drive legally. |
| 61 | +``` |
| 62 | + |
| 63 | +## Logical OR (`||`) |
| 64 | + |
| 65 | +The logical OR operator `||` returns true if at least one of the operands is true. It also uses short-circuit evaluation, meaning if the first operand is true, the second operand is not evaluated. |
| 66 | + |
| 67 | +### Syntax of `||` |
| 68 | + |
| 69 | +```pseudo |
| 70 | +expression1 || expression2 |
| 71 | +``` |
| 72 | + |
| 73 | +### Example of Logical OR (`||`) Operator |
| 74 | + |
| 75 | +```cpp |
| 76 | +#include <iostream> |
| 77 | +using namespace std; |
| 78 | + |
| 79 | +int main() { |
| 80 | + int day = 6; // Saturday |
| 81 | + bool isHoliday = false; |
| 82 | + |
| 83 | + // Check if it's a weekend or holiday |
| 84 | + if (day == 6 || day == 7 || isHoliday) { |
| 85 | + cout << "No work today!" << endl; |
| 86 | + } else { |
| 87 | + cout << "Work day." << endl; |
| 88 | + } |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +The output of this code is: |
| 95 | + |
| 96 | +```shell |
| 97 | +No work today! |
| 98 | +``` |
| 99 | + |
| 100 | +## Logical NOT (`!`) |
| 101 | + |
| 102 | +The logical NOT operator `!` reverses the boolean value of its operand. If the operand is true, it returns false, and vice versa. |
| 103 | + |
| 104 | +### Syntax of `!` |
| 105 | + |
| 106 | +```pseudo |
| 107 | +!expression1 |
| 108 | +``` |
| 109 | + |
| 110 | +### Example of Logical NOT (`!`) Operator |
| 111 | + |
| 112 | +```cpp |
| 113 | +#include <iostream> |
| 114 | +using namespace std; |
| 115 | + |
| 116 | +int main() { |
| 117 | + bool isRaining = false; |
| 118 | + |
| 119 | + if (!isRaining) { |
| 120 | + cout << "Perfect weather for a walk!" << endl; |
| 121 | + } else { |
| 122 | + cout << "Better stay inside." << endl; |
| 123 | + } |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +The output of this code is: |
| 130 | + |
| 131 | +```shell |
| 132 | +Perfect weather for a walk! |
| 133 | +``` |
| 134 | + |
| 135 | +## Codebyte Example |
| 136 | + |
| 137 | +The following Codebyte demonstrates how logical operators are used in a real-world scenario such as verifying login credentials: |
| 138 | + |
| 139 | +```codebyte/cpp |
| 140 | +#include <iostream> |
| 141 | +#include <string> |
| 142 | +using namespace std; |
| 143 | +
|
| 144 | +int main() { |
| 145 | + string username = "admin"; |
| 146 | + string password = "secure123"; |
| 147 | + bool isActive = true; |
| 148 | + int loginAttempts = 2; |
| 149 | + int maxAttempts = 3; |
| 150 | +
|
| 151 | + // Input credentials |
| 152 | + string inputUser = "admin"; |
| 153 | + string inputPass = "secure123"; |
| 154 | +
|
| 155 | + // Check authentication using logical operators |
| 156 | + bool validCredentials = (inputUser == username) && (inputPass == password); |
| 157 | + bool accountAccessible = isActive && (loginAttempts < maxAttempts); |
| 158 | + bool canLogin = validCredentials && accountAccessible; |
| 159 | +
|
| 160 | + cout << "Authentication Results:" << endl; |
| 161 | + cout << "Valid credentials: " << (validCredentials ? "Yes" : "No") << endl; |
| 162 | + cout << "Account accessible: " << (accountAccessible ? "Yes" : "No") << endl; |
| 163 | + cout << "Login successful: " << (canLogin ? "Yes" : "No") << endl; |
| 164 | +
|
| 165 | + // Demonstrate NOT operator |
| 166 | + if (!canLogin) { |
| 167 | + cout << "Access denied!" << endl; |
| 168 | + } else { |
| 169 | + cout << "Welcome to the system!" << endl; |
| 170 | + } |
| 171 | +
|
| 172 | + // Demonstrate OR operator for error handling |
| 173 | + if (!validCredentials || !accountAccessible) { |
| 174 | + cout << "Login failed due to: "; |
| 175 | + if (!validCredentials) cout << "Invalid credentials "; |
| 176 | + if (!accountAccessible) cout << "Account issues "; |
| 177 | + cout << endl; |
| 178 | + } |
| 179 | +
|
| 180 | + return 0; |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +The following table summarizes the three logical operators in C++: |
| 185 | + |
| 186 | +| Symbol | Name | Description | Implementation Example | |
| 187 | +| ------ | ----------- | ------------------------------------------------- | ---------------------- | |
| 188 | +| `&&` | Logical AND | Returns true if both operands are true | `a && b` | |
| 189 | +| `\|\|` | Logical OR | Returns true if at least one operand is true | `a \|\| b` | |
| 190 | +| `!` | Logical NOT | Returns the opposite boolean value of the operand | `!a` | |
0 commit comments