🧠 C++ Logical Operators – Master &&, ||, and !
Logical operators in C++ help you make smart decisions by combining multiple conditions. Perfect for if statements, loops, and any brainy logic! 🤓
🔌 Common Logical Operators
&&
– Logical AND (true if both conditions are true)||
– Logical OR (true if at least one condition is true)!
– Logical NOT (reverses the truth value)
🧪 Example: Logic in Action
#include <iostream> using namespace std; int main() { int a = 5, b = 10; cout << "a > 0 && b > 0: " << (a > 0 && b > 0) << endl; cout << "a > 0 || b < 0: " << (a > 0 || b < 0) << endl; cout << "!(a == b): " << !(a == b) << endl; return 0; }
💬 Explanation
- AND: Both conditions must be true to return true.
- OR: Returns true if at least one condition is true.
- NOT: Flips the result. If true becomes false, false becomes true.
These are the foundation of making decisions in your code. So logical, right? 😄