Contents Index Search Previous Next
5.4 Case Statements
selects for execution one of a number of alternative sequences_of_statements;
the chosen alternative is defined by the value of an expression.
case expression is
Name Resolution Rules
is expected to be of any discrete type.
type for each discrete_choice
the type of the expression
and discrete_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 discrete_choice_list.
The possible values of the expression
shall be covered as follows:
- If the expression
is a name (including a type_conversion
or a function_call) having a static
and constrained nominal subtype, or is a qualified_expression
whose subtype_mark denotes a static
and constrained scalar subtype, then each non-others discrete_choice
shall cover only values in that subtype, and each value of that subtype
shall be covered by some discrete_choice
(either explicitly or by others).
- If the type of the expression
is root_integer, universal_integer, or a descendant of
a formal scalar type, then the case_statement
shall have an others discrete_choice.
- Otherwise, each value of the base
range of the type of the expression
shall be covered (either explicitly or by others).
Two distinct discrete_choices
of a case_statement shall not cover
the same value.
For the execution of a case_statement
is first evaluated.
If the value of the expression
is covered by the discrete_choice_list
of some case_statement_alternative,
then the sequence_of_statements
of the _alternative is executed.
(the value is not covered by any discrete_choice_list
perhaps due to being outside the base range), Constraint_Error is raised.
5 The execution of a case_statement
chooses one and only one alternative. Qualification of the expression
of a case_statement by a static
subtype can often be used to limit the number of choices that need be
case Sensor is
when Elevation => Record_Elevation(Sensor_Value);
when Azimuth => Record_Azimuth (Sensor_Value);
when Distance => Record_Distance (Sensor_Value);
when others => null;
case Today is
when Mon => Compute_Initial_Balance;
when Fri => Compute_Closing_Balance;
when Tue .. Thu => Generate_Report(Today);
when Sat .. Sun => null;
case Bin_Number(Count) is
when 1 => Update_Bin(1);
when 2 => Update_Bin(2);
when 3 | 4 =>
when others => raise Error;
Contents Index Search Previous Next Legal