The following is the general pattern for C++'s value-producing conditional-operator expression:
Boolean expression ? if-true expression : if-false expression
Note that, in contrast to the operators you have seen so far, the
conditional operator consists of a combination of distributed symbols,
?
and :
, separating three operandsthe Boolean expression,
the if-true expression, and the if-false expression. Thus, the
conditional operator combination is said to be a ternary operator
with distributed operator symbols.
Note also the similarity to the if-else
statementit is as though the
if
became a question mark, and moved between the first two
expressions, and the else
became a colon.
Finally, note that either the if-true expression or the if-false expression is evaluated, but both are not. Thus, any variable assignments or other side effects in the unevaluated expression do not occur.