Conditional Expressions
Author: JTC1/SC22/WG9/ARG, by Randall Brukardt, ARG Editor

Ada Reference Manual — Legal Information

# 4.5.7 Conditional Expressions

1/3
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

2/3
conditional_expression ::= if_expression | case_expression
3/3
if_expression ::=
if condition then dependent_ expression
{elsif condition then dependent_ expression}
[else dependent_ expression]
4/3
condition ::= boolean_ expression
5/3
case_expression ::=
case selecting_ expression is
case_expression_alternative {,
case_expression_alternative}
6/3
case_expression_alternative ::=
when discrete_choice_list =>
dependent_ expression
7/3
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

8/3
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.
9/3
The possible types of a conditional_expression are further determined as follows:
10/3
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,
11/3
If all of the dependent_ expressions are of the same type, the type of the conditional_expression is that type; otherwise,
12/3
If a dependent_ expression is of an elementary type, the type of the conditional_expression shall be covered by that type; otherwise,
13/3
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.
14/3
A condition is expected to be of any boolean type.
15/3
The expected type for the selecting_ expression and the discrete_choices are as for case statements (see 5.4).

#### Legality Rules

16/3
All of the dependent_ expressions shall be convertible (see 4.6) to the type of the conditional_expression.
17/3
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.
18/3
If there is no else dependent_ expression, the if_expression shall be of a boolean type.
19/3
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

20/3
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.
21/3
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.

Ada 2005 and 2012 Editions sponsored in part by Ada-Europe