Ada Reference Manual (Ada 2022) — 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.

#### Examples

22/5
Example of use of an if_expression:
23/5
Put_Line ("Casey is " &
(if Casey.Sex = M then "Male" else "Female")); -- see 3.10.1
24/5
Example of use of a case_expression:
25/5
function Card_Color (Card : Suit) return Color is -- see 3.5.1
(case Card is
when Clubs | Spades => Black,
when Hearts | Diamonds => Red);

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