← Back to C++ Course | Chapter 3: Operators | Lesson 4 of 9

C++ के Logical Operators

Logical operators true-or-false questions combine करते हैं, जैसे 'अगर धूप है AND homework हो गया तो आप बाहर जा सकते हैं'। && का मतलब and है, || का मतलब or है, और ! का मतलब not है।
Syntax
cpp
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 (&&)

cpp
// 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;
}

Logical OR (||)

|| (logical OR) true evaluate होता है अगर दो expressions में से कम से कम एक true हो, useful जब भी कई conditions में से कोई एक आगे बढ़ने के लिए काफी हो, जैसे if (isAdmin || isOwner)। यह सिर्फ तब false evaluate होता है जब दोनों sides false हों।

उदाहरण: Logical OR (||)

cpp
// 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;
}

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 (!)

cpp
// 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;
}

Logical Operators Combine करना

Complex conditions कई && और || operators को साथ chain कर सकती हैं, और क्योंकि C++ default से && को || से पहले evaluate करता है (जैसे *, + से tighter bind करता है), parentheses ज़रूरी हैं जब भी आपको default precedence पर depend करने की बजाय intended grouping explicit बनानी हो।

उदाहरण: Combining Logical Operators

cpp
#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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक logical AND के लिए && की बजाय & इस्तेमाल करना, जो एक bitwise operator है और unexpected results दे सकता है।
  2. if (x == 1 || 2) लिखना, जो हमेशा true है क्योंकि 2 non-zero है; आपको x == 1 || x == 2 लिखना होगा।
  3. यह भरोसा करना कि && या || का right side हमेशा चलेगा, जब short-circuiting इसे और इसके किसी भी side effect को skip कर सकती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.