C++ का Ternary Operator
In this page:
variable = (condition) ? value_if_true : value_if_false;
Ternary Operator का Introduction
Ternary operator condition ? valueIfTrue : valueIfFalse एक simple if-else choice को एक single expression में pack करता है, condition true है या नहीं इस आधार पर दो values में से एक evaluate होते हुए।
यह C++ में इकलौता operator है जो तीन operands लेता है, यही वजह है कि इसे conditional operator भी कहते हैं।
उदाहरण: Introduction to Ternary Operator
// 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;
std::string result = (age >= 18) ? "Adult" : "Minor";
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
सीधे Values Assign करना
क्योंकि ternary operator खुद एक statement की बजाय एक expression है, आप इसे सीधे एक assignment के right side पर इस्तेमाल कर सकते हैं, जैसे int max = (a > b) ? a : b;, सिर्फ एक value चुनने के लिए एक अलग multi-line if-else block की ज़रूरत से बचते हुए।
यह simple two-way decisions को compact और readable रखता है।
उदाहरण: Assigning Values Directly
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 10, b = 20;
// Declare max and set it to (a > b) ? a : b
int max = (a > b) ? a : b;
// Print to the console with cout
std::cout << max << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Ternary Operators
एक ternary operator को दूसरे के अंदर nest करना आपको दो से ज़्यादा outcomes handle करने देता है, जैसे grade = (score >= 90) ? A : (score >= 80) ? B : C;, लेकिन दो levels nesting के बाद readability जल्दी गिरती है।
एक simple three-way choice से आगे किसी भी चीज़ के लिए, एक if-else ladder आमतौर पर ज़्यादा clear है।
उदाहरण: Nested Ternary Operators
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 85
int score = 85;
char grade = (score >= 90) ? 'A' : (score >= 80) ? 'B' : 'C';
// Print to the console with cout
std::cout << grade << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Ternary Operator से सीधे Print करना
cout के अंदर सीधे एक ternary expression embed करना, जैसे cout << (isEven ? "Even" : "Odd");, आपको सिर्फ text चुनने के लिए एक अलग if-else block लिखे बिना दो messages में से एक print करने देता है।
यह इसलिए काम करता है क्योंकि cout << एक expression की उम्मीद करता है, और एक ternary operator exactly वही evaluate होता है।
उदाहरण: Printing directly with Ternary Operator
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare num and set it to 7
int num = 7;
bool isEven = (num % 2 == 0);
// Print to the console with cout
std::cout << (isEven ? "Even" : "Odd") << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Ternary Operator के साथ Math Calculations
आप एक ternary की किसी भी branch के अंदर सीधे arithmetic डाल सकते हैं, जैसे total = (isMember) ? price * 0.9 : price;, condition के आधार पर एक line में एक discounted या full price compute करते हुए।
यह छोटी conditional calculations को उनके आस-पास एक पूरे if-else block की ज़रूरत के बिना self-contained रखता है।
उदाहरण: Ternary Operator with Math Calculations
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare price and set it to 50.0
double price = 50.0;
// Declare isMember and set it to true
bool isMember = true;
double total = (isMember) ? price * 0.9 : price;
// Print to the console with cout
std::cout << total << std::endl;
// 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 unreadable हो जाए। - दोनों branches में अलग incompatible types इस्तेमाल करना, जैसे एक string और एक int, जो compile होने में fail होता है।
cout << x > 5 ? "a" : "b"में parentheses भूल जाना, जहाँ<<tighter bind करता है और code वह नहीं करता जो आपने चाहा था।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: