C++ Logical Operators in Conditions

🔗 C++ Logical Operators in Conditions

C++ mein logical operators ka use multiple conditions ko combine karne ke liye hota hai—especially if, else if, while, for conditions mein.


🔹 1. Logical Operators List

OperatorNameMeaning
&&ANDDono conditions true
``
!NOTCondition ko ulta

🔹 2. Logical AND (&&) in Condition

int age = 20;
bool hasID = true;
if (age >= 18 && hasID) {
cout << “Entry Allowed”;
}

Tabhi true jab dono conditions true hon.


🔹 3. Logical OR (||) in Condition

int marks = 35;

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

Koi ek condition true ho to block execute hoga.


🔹 4. Logical NOT (!) in Condition

bool isBlocked = false;

if (!isBlocked) {
cout << “Access Granted”;
}

falsetrue


🔹 5. Multiple Conditions Together

int age = 25;
bool hasTicket = true;
bool hasID = false;
if (age >= 18 && (hasTicket || hasID)) {
cout << “Allowed”;
}

👉 Parentheses clarity ke liye use karein.


🔹 6. Logical Operators with Comparisons

int a = 10, b = 5;

if (a > b && a > 0) {
cout << “Valid”;
}


🔹 7. Logical Operators in Loops

int i = 1;

while (i >= 1 && i <= 5) {
cout << i << ” “;
i++;
}


🔹 8. Short-Circuit Evaluation ⚡

int x = 0;

if (x != 0 && (10 / x > 2)) {
cout << “Safe”;
}

✔ Second condition evaluate nahi hoti (division by zero avoid).


❌ Common Mistakes

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

✔ Correct:

if (a > 5 && a < 10)

📌 Summary

  • && → all true

  • || → any one true

  • ! → negation

  • Parentheses improve clarity

  • Short-circuit evaluation important

You may also like...