C++ Core
Ternary Operator
The conditional operator — pick a value inline with cond ? a : b.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- Real name = conditional operator; the only operator with three operands. Syntax:
cond ? a : b. - It's an expression (yields a value), not a statement — so it works inline: init a variable, a
const, areturn, a function argument. - Only the chosen branch is evaluated (safe for guarded access like
p ? *p : 0). - Both branches must share a common type; if they differ but convert (int/double) the result is the common type — no common type = error.
- Low precedence: wrap it in parentheses with
<<->cout << (c ? "a" : "b");. - Nesting/chaining is legal but hurts readability; prefer
if/elseorswitchbeyond one condition.
One example that shows the key points
int a = 3, b = 7;
int max = (a > b) ? a : b; // expression -> initialize a variable
const int x = (a < b) ? 5 : 15; // works for a const (one-shot init)
int* p = nullptr;
int y = p ? *p : 0; // only the chosen branch runs -> safe
auto v = (a < b) ? 1 : 2.5; // int -> double: result type is double
std::cout << (a < b ? "yes" : "no"); // parentheses: << binds tighter than ?: Watch out
Easily confused
ternary vs if/else
Ternary is an expression that yields a value (works inline, can init a const). if/else runs statements and returns nothing.
same type vs common type
Branches don't need the SAME type — they need a COMMON type. int/double -> result is double; int/string-literal -> error.
?: vs << precedence
<< binds tighter than ?:, so wrap the ternary in parentheses inside a cout.Test yourself
8 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.