C++ Ternary Operator
In this page:
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
// 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
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
// 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
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
// 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
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
// 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 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
// 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
- Nesting several
?:operators so deeply that the code becomes unreadable. - Using different incompatible types in the two branches, like a string and an int, which fails to compile.
- Forgetting parentheses in
cout << x > 5 ? "a" : "b", where<<binds tighter and the code does not do what you intended.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: