C में Logical Conditions
In this page:
if (condition1 && condition2) {
// both are true
}
if (condition1 || condition2) {
// at least one is true
}
if (!condition) {
// condition is false
}
Conditions में Logical Operators
Logical operators कई individual conditions को एक single expression में combine करते हैं जिसे एक if statement test कर सकता है, एक decision को एक साथ एक से ज़्यादा factor पर depend करने देते हुए।
उदाहरण: Logical Operators in Conditions
// 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 hasLicense and set it to 1
int hasLicense = 1;
// Check whether age >= 18 && hasLicense
if (age >= 18 && hasLicense) {
// Print formatted text to the screen
printf("Can drive");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
&& से Conditions Combine करना
&& (logical AND) operator दो conditions को combine करता है ताकि overall expression सिर्फ तब true हो जब दोनों individual conditions true हों, आमतौर पर यह check करने के लिए इस्तेमाल होता है कि एक value एक specific range में आती है।
उदाहरण: Combining Conditions with &&
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 75
int score = 75;
// Check whether score >= 60 && score <= 100
if (score >= 60 && score <= 100) {
// Print formatted text to the screen
printf("In range");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
|| से Conditions Combine करना
|| (logical OR) operator दो conditions को combine करता है ताकि overall expression true हो जब इनमें से कम से कम एक true हो, यह check करने के लिए useful कि एक value कई acceptable options में से किसी से match करती है।
उदाहरण: Combining Conditions with ||
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare grade and set it to 'A'
char grade = 'A';
// 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
! से एक Condition Negate करना
! (logical NOT) operator एक single condition को invert करता है, एक true value को false और एक false value को true बनाते हुए, जिसे अक्सर एक condition के नाम के आगे naturally not की तरह पढ़ा जाता है।
उदाहरण: Negating a Condition with !
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare isBanned and set it to 0
int isBanned = 0;
// Check whether !isBanned
if (!isBanned) {
// Print formatted text to the screen
printf("Access allowed");
}
// 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 && और || को short-circuit evaluation से evaluate करता है: && के लिए, अगर पहली condition false है, दूसरी evaluate ही नहीं होती, जो आमतौर पर division by zero जैसे operations से safely guard करने के लिए इस्तेमाल होता है।
उदाहरण: Short-Circuit Evaluation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare divisor and set it to 0
int divisor = 0;
// Check whether divisor != 0 && (10 / divisor > 1)
if (divisor != 0 && (10 / divisor > 1)) {
// Print formatted text to the screen
printf("unreachable");
} else {
// Print formatted text to the screen
printf("Avoided division by zero");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
x > 1 && < 10लिखना और दूसराxछोड़ देना, जो एक syntax error है।&&को||से confuse करना, ताकि जिस condition को दोनों checks चाहिए वह किसी एक से भी pass हो जाए।&&और||मिलाते समय parentheses भूल जाना, क्योंकि&&tighter bind करता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: