← Back to C++ Course | Chapter 3: Operators | Lesson 8 of 9

C++ का Ternary Operator

Ternary operator एक question के आधार पर दो answers में से चुनने का एक one-line तरीका है, जैसे 'अगर बारिश हो तो umbrella ले लो, नहीं तो sunglasses'। यह एक short if-else है।
Syntax
cpp
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

cpp
// 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;
}

सीधे 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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
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. कई ?: operators को इतना गहरा nest करना कि code unreadable हो जाए।
  2. दोनों branches में अलग incompatible types इस्तेमाल करना, जैसे एक string और एक int, जो compile होने में fail होता है।
  3. cout << x > 5 ? "a" : "b" में parentheses भूल जाना, जहाँ << tighter bind करता है और code वह नहीं करता जो आपने चाहा था।

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.