C++ Logical Operators

πŸ”— C++ Logical Operators

Logical operators ka use conditions ko combine ya negate karne ke liye hota hai.
Ye operators zyada tar if-else, loops, decision making mein use hote hain.


πŸ”Ή 1. List of Logical Operators

Operator Name Meaning
&& Logical AND Dono conditions true honi chahiye
! Logical NOT Condition ko ulta kar deta hai

πŸ”Ή 2. Logical AND (&&)

int age = 20;

if (age >= 18 && age <= 60) {
cout << “Eligible”;
}

βœ” Result true tabhi hoga jab dono conditions true hon.


πŸ”Ή 3. Logical OR (||)

int marks = 35;

if (marks >= 40 || marks >= 33) {
cout << “Pass”;
}

βœ” Agar koi ek condition true hai to result true.


πŸ”Ή 4. Logical NOT (!)

bool isLoggedIn = false;

if (!isLoggedIn) {
cout << “Please login”;
}

βœ” true β†’ false
βœ” false β†’ true


πŸ”Ή 5. Logical Operators with Comparison

int a = 10, b = 5;

cout << (a > b && a > 0); // 1
cout << (a < b || b > 0); // 1
cout << !(a == b); // 1


πŸ”Ή 6. Using boolalpha (Readable Output)

cout << boolalpha << (5 > 3 && 2 > 1);

Output:

true

πŸ”Ή 7. Truth Table

AND (&&)

A B Result
T T T
T F F
F T F
F F F

OR (||)

A B Result
T T T
T F T
F T T
F F F

NOT (!)

A Result
T F
F T

❌ Common Mistakes

if (a > 5 && < 10) // ❌ wrong

βœ” Correct:

if (a > 5 && a < 10)

πŸ“Œ Summary

  • Logical operators conditions combine karte hain

  • && = AND, || = OR, ! = NOT

  • Mostly used with comparison operators

You may also like...