C का Ternary Operator
In this page:
variable = (condition) ? value_if_true : value_if_false;
Ternary Operator क्या है?
Ternary operator condition ? valueIfTrue : valueIfFalse syntax इस्तेमाल करके एक simple if-else को एक expression में condense करता है, जो खासकर तब handy है जब आपको statements के अलग blocks चलाने की बजाय सिर्फ दो values में से एक चुनना हो।
उदाहरण: What is the Ternary Operator?
// 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;
char *status = (age >= 18) ? "Adult" : "Minor";
// Print formatted text to the screen
printf("%s", status);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Maximum ढूंढना
दो numbers में से बड़ा ढूंढना एक line में max = (a > b) ? a : b; की तरह लिखा जा सकता है, दो values के बीच fundamentally simple choice के लिए एक पूरा four-line if-else block बनाने से बचते हुए।
उदाहरण: Finding the Maximum
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 7, b = 12;
// Declare max and set it to (a > b) ? a : b
int max = (a > b) ? a : b;
// Print formatted text to the screen
printf("%d", max);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pass या Fail Check
pass/fail जैसे binary outcomes के लिए, ternary operator आपको सीधे एक result compute करने देता है: result = (score >= 50) ? "Pass" : "Fail";, logic को एक single assignment में compact और readable रखते हुए।
उदाहरण: Pass or Fail Check
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 65
int score = 65;
char *result = (score >= 50) ? "Pass" : "Fail";
// Print formatted text to the screen
printf("%s", result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Even या Odd
n % 2 == 0 को ternary operator के साथ combine करना आपको एक number को even या odd के तौर पर एक single expression में classify करने देता है, जैसे label = (n % 2 == 0) ? "Even" : "Odd";, बिना एक अलग if statement के।
उदाहरण: Even or Odd
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 7
int n = 7;
char *label = (n % 2 == 0) ? "Even" : "Odd";
// Print formatted text to the screen
printf("%s", label);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Ternary
आप दो से ज़्यादा outcomes handle करने के लिए ternary operators nest कर सकते हैं, लेकिन nesting का हर level expression को एक नज़र में पढ़ना मुश्किल बना देता है -- दो conditions के बाद, एक regular if-else ladder आमतौर पर ज़्यादा clear होता है।
उदाहरण: Nested Ternary
// 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;
char *grade = (score >= 90) ? "A" : (score >= 70) ? "B" : "C";
// Print formatted text to the screen
printf("%s", grade);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- कई
?:operators को इतना गहरा nest करना कि code if-else chain के मुकाबले unreadable हो जाए। a + b > c ? x : yजैसे बड़े expression में parentheses भूल जाना, जहाँ precedence एक unexpected grouping देती है।- अलग types produce करने वाली branches इस्तेमाल करना, जैसे एक string और एक int, जो compile नहीं होता या warnings देता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: