C++ के Logical Operators
In this page:
condition1 && condition2 // AND
condition1 || condition2 // OR
!condition // NOT
Logical AND (&&)
&& (logical AND) सिर्फ तब true evaluate होता है जब आस-पास के दोनों expressions true हों, इसे सही choice बनाते हुए जब भी एक condition को genuinely दो चीज़ें एक साथ true होने की ज़रूरत हो, जैसे if (age >= 18 && hasID)।
अगर कोई एक side false हो, दूसरे की परवाह किए बिना पूरा expression false है।
उदाहरण: Logical AND (&&)
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 20
int age = 20;
// Declare hasID and set it to true
bool hasID = true;
// Check whether age >= 18 && hasID
if (age >= 18 && hasID) {
// Print to the console with cout
std::cout << "Access granted" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Logical OR (||)
|| (logical OR) true evaluate होता है अगर दो expressions में से कम से कम एक true हो, useful जब भी कई conditions में से कोई एक आगे बढ़ने के लिए काफी हो, जैसे if (isAdmin || isOwner)। यह सिर्फ तब false evaluate होता है जब दोनों sides false हों।
उदाहरण: Logical OR (||)
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
bool isAdmin = false, isOwner = true;
// Check whether isAdmin || isOwner
if (isAdmin || isOwner) {
// Print to the console with cout
std::cout << "Access granted" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Logical NOT (!)
! (logical NOT) एक boolean की value flip करता है, true को false में बदलते हुए और vice versa, और सबसे ज़्यादा एक condition को cleanly invert करने के लिए इस्तेमाल होता है, जैसे if (!isEmpty(list)) naturally 'अगर list empty नहीं है' की तरह पढ़ता है।
! से double negatives को overuse करना readability को नुकसान पहुंचा सकता है, इसलिए boolean variables को इस तरह नाम देना worth है कि positive case clearly पढ़े।
उदाहरण: Logical NOT (!)
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isEmpty and set it to false
bool isEmpty = false;
// Check whether !isEmpty
if (!isEmpty) {
// Print to the console with cout
std::cout << "List is not empty" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Logical Operators Combine करना
Complex conditions कई && और || operators को साथ chain कर सकती हैं, और क्योंकि C++ default से && को || से पहले evaluate करता है (जैसे *, + से tighter bind करता है), parentheses ज़रूरी हैं जब भी आपको default precedence पर depend करने की बजाय intended grouping explicit बनानी हो।
उदाहरण: Combining Logical Operators
#include <iostream>
int main() {
bool a = true, b = false, c = true;
if ((a || b) && c) { // parentheses make the grouping explicit
std::cout << "Condition met" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Short-circuit Evaluation
C++ short-circuit evaluation इस्तेमाल करता है: a && b में, अगर a false है पूरा expression पहले से false जाना जाता है, इसलिए b कभी evaluate ही नहीं होता — a || b में a true होने पर भी यही apply होता है।
यह सिर्फ एक optimization नहीं है; यह आमतौर पर जानबूझकर इस्तेमाल होता है, जैसे if (ptr != nullptr && ptr->value > 0) में, जहाँ दूसरे check को safe बनाने के लिए null check पहले चलना ज़रूरी है।
उदाहरण: Short-circuit Evaluation
#include <iostream>
int main() {
int* ptr = nullptr;
if (ptr != nullptr && *ptr > 0) { // null check runs first, safely
std::cout << "Positive" << std::endl;
} else {
std::cout << "Skipped dereference safely" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक logical AND के लिए
&&की बजाय&इस्तेमाल करना, जो एक bitwise operator है और unexpected results दे सकता है। if (x == 1 || 2)लिखना, जो हमेशा true है क्योंकि2non-zero है; आपकोx == 1 || x == 2लिखना होगा।- यह भरोसा करना कि
&&या||का right side हमेशा चलेगा, जब short-circuiting इसे और इसके किसी भी side effect को skip कर सकती है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: