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

C++ Ternary Operator

The ternary operator is a one-line way to pick between two answers based on a question, like 'if it rains take an umbrella, otherwise sunglasses'. It is a short if-else.
Syntax
cpp
variable = (condition) ? value_if_true : value_if_false;

Introduction to Ternary Operator

The ternary operator condition ? valueIfTrue : valueIfFalse packs a simple if-else choice into a single expression, evaluating to one of two values depending on whether the condition is true.

It's the only operator in C++ that takes three operands, which is why it's also called the conditional operator.

Example: 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;
}

Assigning Values Directly

Because the ternary operator is itself an expression rather than a statement, you can use it directly on the right side of an assignment, like int max = (a > b) ? a : b;, avoiding the need for a separate multi-line if-else block just to pick a value.

This keeps simple two-way decisions compact and readable.

Example: 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

Nesting one ternary operator inside another lets you handle more than two outcomes, such as grade = (score >= 90) ? A : (score >= 80) ? B : C;, but readability drops fast past two levels of nesting.

For anything beyond a simple three-way choice, an if-else ladder is usually clearer.

Example: 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;
}

Printing directly with Ternary Operator

Embedding a ternary expression directly inside cout, like cout << (isEven ? "Even" : "Odd");, lets you print one of two messages without writing a separate if-else block just to choose the text.

This works because cout << expects an expression, and a ternary operator evaluates to exactly that.

Example: 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 with Math Calculations

You can put arithmetic directly inside either branch of a ternary, such as total = (isMember) ? price * 0.9 : price;, computing a discounted or full price in one line depending on the condition.

This keeps small conditional calculations self-contained without needing a full if-else block around them.

Example: 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. #}
Common Mistakes
  1. Nesting several ?: operators so deeply that the code becomes unreadable.
  2. Using different incompatible types in the two branches, like a string and an int, which fails to compile.
  3. Forgetting parentheses in cout << x > 5 ? "a" : "b", where << binds tighter and the code does not do what you intended.

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.