5.4. Case Statements

[PREVIOUS][UP][NEXT]

A 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.

    case_statement ::=
       case expression is
           case_statement_alternative
          {case_statement_alternative}
       end case; 

    case_statement_alternative ::=
       when choice {| choice } =>
          sequence_of_statements 

The expression must be of a discrete type which must be determinable independently of the context in which the expression occurs, but using the fact that the expression must be of a discrete type. Moreover, the type of this expression must not be a generic formal type. Each choice in a case statement alternative must be of the same type as the expression; the list of choices specifies for which values of the expression the alternative is chosen.

If the expression is the name of an object whose subtype is static, then each value of this subtype must be represented once and only once in the set of choices of the case statement, and no other value is allowed; this rule is likewise applied if the expression is a qualified expression or type conversion whose type mark denotes a static subtype. Otherwise, for other forms of expression, each value of the (base) type of the expression must be represented once and only once in the set of choices, and no other value is allowed.

The simple expressions and discrete ranges given as choices in a case statement must be static. A choice defined by a discrete range stands for all values in the corresponding range (none if a null range). The choice others is only allowed for the last alternative and as its only choice; it stands for all values (possibly none) not given in the choices of previous alternatives. A component simple name is not allowed as a choice of a case statement alternative.

The execution of a case statement consists of the evaluation of the expression followed by the execution of the chosen sequence of statements.

Examples:

    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; 

    case TODAY is
       when MON        => COMPUTE_INITIAL_BALANCE;
       when FRI        => COMPUTE_CLOSING_BALANCE;
       when TUE .. THU => GENERATE_REPORT(TODAY);
       when SAT .. SUN => null;
    end case; 

    case 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; 

Notes:

The execution of a case statement chooses one and only one alternative, since the choices are exhaustive and mutually exclusive. 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 given explicitly.

An others choice is required in a case statement if the type of the expression is the type universal_integer (for example, if the expression is an integer literal), since this is the only way to cover all values of the type universal_integer.

References: base type, choice, context of overload resolution, discrete type, expression, function call, generic formal type, conversion, discrete type, enumeration literal, expression, name, object, overloading, and 8.7, qualified expression, sequence of statements, static discrete range, static subtype, subtype, type, type conversion, type mark.


[INDEX][CONTENTS]