← 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
c
condition1 && condition2  // AND
condition1 || condition2  // OR
!condition                // NOT

Logical AND (&&)

&& (logical AND) सिर्फ तब true evaluate होता है जब left और right दोनों conditions true हों -- जब भी कोई action सिर्फ तब होनी चाहिए जब कई requirements एक साथ satisfy हों, जैसे कि यह check करना कि एक value positive भी है और एक limit से नीचे भी है, तब useful।

उदाहरण: Logical AND (&&)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Declare hasID and set it to 1
	int hasID = 1;
	// Check whether age >= 18 && hasID
	if (age >= 18 && hasID) {
		// Print formatted text to the screen
		printf("Allowed");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical OR (||)

|| (logical OR) true evaluate होता है अगर इसकी दो conditions में से कम से कम एक true हो -- कई valid cases में से किसी को भी accept करने के लिए useful, जैसे यह check करना कि input एक option से match करता है या दूसरे से।

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

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare grade and set it to 'B'
	char grade = 'B';
	// Check whether grade == 'A' || grade == 'B'
	if (grade == 'A' || grade == 'B') {
		// Print formatted text to the screen
		printf("Good grade");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical NOT (!)

! (logical NOT) एक condition की truth value flip करता है: true किसी चीज़ पर apply होने पर यह false बन जाती है, और false किसी चीज़ पर apply होने पर यह true बन जाती है -- पूरे check को restructure करने की बजाय एक condition को "not X" की तरह लिखने के लिए handy।

उदाहरण: Logical NOT (!)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare isLoggedIn and set it to 0
	int isLoggedIn = 0;
	// Check whether !isLoggedIn
	if (!isLoggedIn) {
		// Print formatted text to the screen
		printf("Please log in");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Short-Circuit Evaluation

C के && और || operators short-circuit करते हैं: a && b में, अगर a पहले से false है, b का evaluate ही नहीं होता, क्योंकि overall result पहले ही decide हो चुका है -- यह मायने रखता है जब b के side effects हों या ज़रूरत के बिना evaluate होने पर एक error cause कर सकता हो।

उदाहरण: Short-Circuit Evaluation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 0
	int a = 0;
	// Check whether a != 0 && (10 / a > 1)
	if (a != 0 && (10 / a > 1)) {
		// Print formatted text to the screen
		printf("unreachable");
	}
	// Print formatted text to the screen
	printf("Safe: division skipped");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical Operators Combine करना

बिना parentheses के एक expression में कई logical operators combine करना C के default precedence rules पर depend करता है, जिन्हें गलत समझना आसान है -- हर sub-condition को parentheses में wrap करना intended grouping को एक नज़र में unambiguous बना देता है।

उदाहरण: Combining Logical Operators

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Declare hasTicket and set it to 1
	int hasTicket = 1;
	// Run this block only if the condition is true
	if ((age >= 18) && (hasTicket || age >= 65)) {
		// Print formatted text to the screen
		printf("Entry allowed");
	}
	// Return 0 to signal that the program finished successfully
	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. && की बजाय & इस्तेमाल करना, जो एक bitwise operator है और अलग behave कर सकता है।
  2. if (x == 1 || 2) लिखना, जो हमेशा true है क्योंकि 2 non-zero है।
  3. यह भरोसा करना कि && या || का right side हमेशा चलेगा जब short-circuiting इसे 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.