5.4 Case Statements
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
case_statement selects for execution one of a number of alternative
sequences_of_statements; the chosen alternative is defined by the value of an expression.]
Name Resolution Rules4/3
ranges given as
discrete_choices of a
case_statement shall be static. [A
discrete_choice others, if present, shall appear alone and in the last
- If the selecting_
function_call)] having a static and constrained nominal subtype, then each non-others
discrete_choiceshall cover only values in that subtype that satisfy its predicates (see 3.2.4), and each value of that subtype that satisfies its predicates shall be covered by some
discrete_choice[(either explicitly or by others)].
- If the type of the selecting_
expressionis root_integer, universal_integer, or a descendant of a formal scalar type, then the
case_statementshall have an others
- Otherwise, each value of the base range of the type of the selecting_
expressionshall be covered [(either explicitly or by others)].
- If the expression reads an object with an invalid representation (e.g. an uninitialized object), then the value can be outside the covered range. This can happen for static constrained subtypes, as well as nonstatic or unconstrained subtypes. It cannot, however, happen if the
discrete_choiceothers, because others covers all values, even those outside the subtype. 10.c/3
- If the compiler chooses to represent the value of an expression of an unconstrained subtype in a way that includes values outside the bounds of the subtype, then those values can be outside the covered range. For example, if X: Integer := Integer'Last;, and the case selecting_
expressionis X+1, then the implementation might choose to produce the correct value, which is outside the bounds of Integer. (It might raise Constraint_Error instead.) This case can only happen for nongeneric subtypes that are either unconstrained or nonstatic (or both). It can only happen if there is no others
expression(as usual), or it may choose to correctly evaluate the
expressionand therefore choose the others alternative. Otherwise (no others), Constraint_Error is raised either way — on the
expressionevaluation, or for the
Otherwise (the value is not covered by any
discrete_choice_list, perhaps due to being outside the base range), Constraint_Error is raised.
Examples of case statements:
case Sensor is when Elevation => Record_Elevation(Sensor_Value); when Azimuth => Record_Azimuth (Sensor_Value); when Distance => Record_Distance (Sensor_Value); when others => null; end case; 17case Today is when Mon => Compute_Initial_Balance; when Fri => Compute_Closing_Balance; when Tue .. Thu => Generate_Report(Today); when Sat .. Sun => null; end case; 18case Bin_Number(Count) is when 1 => Update_Bin(1); when 2 => Update_Bin(2); when 3 | 4 => Empty_Bin(1); Empty_Bin(2); when others => raise Error; end case;
Incompatibilities With Ada 83
names, whereas in Ada 83, they were
expressions. Therefore, if the
type_conversion, and the result subtype is static, it is illegal to specify a choice outside the bounds of the subtype. For this case in Ada 83 choices only are required to be in the base range of the type.
type_conversion, Ada 83 required covering all choices in the base range, while Ada 95 only requires covering choices in the bounds of the subtype. If the
case_statementdoes not include an others
discrete_choice, then a legal Ada 83
case_statementwill be illegal in Ada 95 if the bounds of the subtype are different than the bounds of the base type.
Extensions to Ada 83
subtype S is Integer range 1..2;
function F return S;
case F is
when 1 => ...;
when 2 => ...;
-- No others needed.
renaming_declarationis ignored; for a
case_statementwhose expression calls a such a function, the full coverage rules are checked using the result subtype of the original function. Note that predefined operators such as "+" have an unconstrained result subtype (see 4.5.1). Note that generic formal functions do not have static result subtypes. Note that the result subtype of an inherited subprogram need not correspond to any nameable subtype; there is still a perfectly good result subtype, though.
Wording Changes from Ada 83
expressionis now a complete context. See 8.6, “The Context of Overload Resolution”.