Conditional Expression
A conditional expression selects between two branch expressions based on a condition.
Process
At runtime:
- the condition is evaluated;
- if it yields
true, the then-expression is evaluated, and its value is the conditional's value; and - otherwise, the else-expression is evaluated, and its value is the conditional's value.
Only the selected branch is evaluated.
Assertions
- The condition's type is
bool. - The branches have a common type; the conditional expression has that type.
- When an expected type exists, each branch's expected type is that type.
Syntax
conditional_expression = logical_or_expression, [ question_mark, conditional_expression, colon, conditional_expression ] ;