C के Logical Operators
In this page:
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 (&&)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Logical OR (||)
|| (logical OR) true evaluate होता है अगर इसकी दो conditions में से कम से कम एक true हो -- कई valid cases में से किसी को भी accept करने के लिए useful, जैसे यह check करना कि input एक option से match करता है या दूसरे से।
उदाहरण: Logical OR (||)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Logical NOT (!)
! (logical NOT) एक condition की truth value flip करता है: true किसी चीज़ पर apply होने पर यह false बन जाती है, और false किसी चीज़ पर apply होने पर यह true बन जाती है -- पूरे check को restructure करने की बजाय एक condition को "not X" की तरह लिखने के लिए handy।
उदाहरण: Logical NOT (!)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Logical Operators Combine करना
बिना parentheses के एक expression में कई logical operators combine करना C के default precedence rules पर depend करता है, जिन्हें गलत समझना आसान है -- हर sub-condition को parentheses में wrap करना intended grouping को एक नज़र में unambiguous बना देता है।
उदाहरण: Combining Logical Operators
// 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;
}
Login to try C/C++/Java/PHP code in the editor
&&की बजाय&इस्तेमाल करना, जो एक bitwise operator है और अलग behave कर सकता है।if (x == 1 || 2)लिखना, जो हमेशा true है क्योंकि2non-zero है।- यह भरोसा करना कि
&&या||का right side हमेशा चलेगा जब short-circuiting इसे skip कर सकता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: