Ada 2012 Reference Manual
4.5.7 Conditional Expressions
A conditional_expression selects for evaluation at most one of the enclosed dependent_expressions, depending on a decision among the alternatives. One kind of conditional_expression is the if_expression, which selects for evaluation a dependent_expression depending on the value of one or more corresponding conditions. The other kind of conditional_expression is the case_expression, which selects for evaluation one of a number of alternative dependent_expressions; the chosen alternative is determined by the value of a selecting_expression.
Syntax
if_expression ::=
if condition then dependent_expression
{elsif condition then dependent_expression}
[else dependent_expression]
if condition then dependent_expression
{elsif condition then dependent_expression}
[else dependent_expression]
case_expression ::=
case selecting_expression is
case_expression_alternative {,
case_expression_alternative}
case selecting_expression is
case_expression_alternative {,
case_expression_alternative}
Wherever the Syntax Rules allow an expression, a conditional_expression may be used in place of the expression, so long as it is immediately surrounded by parentheses.
Name Resolution Rules
If a conditional_expression is expected to be of a type T, then each dependent_expression of the conditional_expression is expected to be of type T. Similarly, if a conditional_expression is expected to be of some class of types, then each dependent_expression of the conditional_expression is subject to the same expectation. If a conditional_expression shall resolve to be of a type T, then each dependent_expression shall resolve to be of type T.
The possible types of a conditional_expression are further determined as follows:
- If the conditional_expression is the operand of a type conversion, the type of the conditional_expression is the target type of the conversion; otherwise,
- If all of the dependent_expressions are of the same type, the type of the conditional_expression is that type; otherwise,
- If a dependent_expression is of an elementary type, the type of the conditional_expression shall be covered by that type; otherwise,
- If the conditional_expression is expected to be of type T or shall resolve to type T, then the conditional_expression is of type T.
A condition is expected to be of any boolean type.
The expected type for the selecting_expression and the discrete_choices are as for case statements (see 5.4).
Legality Rules
All of the dependent_expressions shall be convertible (see 4.6) to the type of the conditional_expression.
If the expected type of a conditional_expression is a specific tagged type, all of the dependent_expressions of the conditional_expression shall be dynamically tagged, or none shall be dynamically tagged. In this case, the conditional_expression is dynamically tagged if all of the dependent_expressions are dynamically tagged, is tag-indeterminate if all of the dependent_expressions are tag-indeterminate, and is statically tagged otherwise.
If there is no else dependent_expression, the if_expression shall be of a boolean type.
All Legality Rules that apply to the discrete_choices of a case_statement (see 5.4) also apply to the discrete_choices of a case_expression except within an instance of a generic unit.
Dynamic Semantics
For the evaluation of an if_expression, the condition specified after if, and any conditions specified after elsif, are evaluated in succession (treating a final else as elsif True then), until one evaluates to True or all conditions are evaluated and yield False. If a condition evaluates to True, the associated dependent_expression is evaluated, converted to the type of the if_expression, and the resulting value is the value of the if_expression. Otherwise (when there is no else clause), the value of the if_expression is True.
For the evaluation of a case_expression, the selecting_expression is first evaluated. If the value of the selecting_expression is covered by the discrete_choice_list of some case_expression_alternative, then the dependent_expression of the case_expression_alternative is evaluated, converted to the type of the case_expression, and the resulting value is the value of the case_expression. Otherwise (the value is not covered by any discrete_choice_list, perhaps due to being outside the base range), Constraint_Error is raised.