1. Intro
The ternary operator, written condition ? expr1 : expr2, is C++'s only operator that takes three operands. It is a compact expression form of an if-else statement: if condition is true, the whole expression evaluates to expr1; otherwise it evaluates to expr2. Because it's an expression (not a statement), it can be used directly inside assignments, function calls, or cout statements.
Cricket analogy: The ternary wicketsLeft > 3 ? "declare" : "bat on" is like a captain making a split-second call mid-over - one quick expression replacing a whole paragraph of if-else reasoning about the innings.
2. Syntax
result = (condition) ? valueIfTrue : valueIfFalse;3. Explanation
The ternary operator evaluates condition first. If it's true, only expr1 is evaluated (and becomes the result); expr2 is skipped entirely. If condition is false, only expr2 is evaluated. This means the ternary operator also short-circuits — exactly one of the two branch expressions runs, never both. It's best used for simple, single-value selection; nesting many ternary operators for complex logic hurts readability and an if-else chain is usually clearer.
Cricket analogy: Like an umpire who checks only the front-foot no-ball line first - if it's a no-ball, the 'wicket' branch never even gets evaluated - the ternary checks condition and executes only one branch, never both.
Only one branch of the ternary operator is ever evaluated, similar to short-circuiting. int x = (n != 0) ? (100 / n) : 0; is safe because 100 / n is never evaluated when n == 0, avoiding a division-by-zero.
4. Example
#include <iostream>
using namespace std;
int main() {
int age = 20;
string status = (age >= 18) ? "Adult" : "Minor";
cout << "Status: " << status << endl;
int n = 0;
int result = (n != 0) ? (100 / n) : 0;
cout << "Result: " << result << endl;
return 0;
}5. Output
Status: Adult
Result: 06. Key Takeaways
- The ternary operator
condition ? expr1 : expr2is a compact, expression-based alternative toif-else. - It is the only ternary (three-operand) operator in C++.
- Only the selected branch expression is evaluated — the other is skipped, similar to short-circuit behavior.
- Avoid deeply nested ternary chains; prefer
if-elsefor complex multi-branch logic.
Practice what you learned
1. What does `int x = (5 > 3) ? 10 : 20;` set `x` to?
2. How many operands does the ternary operator take?
3. In `int result = (n != 0) ? (100 / n) : 0;`, what happens when `n` is 0?
4. Which of these is the ternary operator equivalent to?
5. Is it good practice to deeply nest multiple ternary operators for complex branching logic?
Was this page helpful?
You May Also Like
if-else Statement in C++
Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.
Logical Operators in C++
Master the C++ logical operators &&, ||, and !, and understand short-circuit evaluation and how it can skip side effects.
Relational Operators in C++
Understand C++ relational (comparison) operators such as ==, !=, <, >, <=, and >=, and how they always evaluate to a bool result.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics