Lady Ada

Ada '83 Language Reference Manual

Copyright 1980, 1982, 1983 owned by the United States Government. Direct reproduction and usage requests to the Ada Information Clearinghouse.

5.4. Case Statements


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

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

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.


    case SENSOR is
       when others     => null;
    end case; 

    case TODAY is
       when MON        => COMPUTE_INITIAL_BALANCE;
       when FRI        => COMPUTE_CLOSING_BALANCE;
       when SAT .. SUN => null;
    end case; 

    case BIN_NUMBER(COUNT) is
       when 1      => UPDATE_BIN(1);
       when 2      => UPDATE_BIN(2);
       when 3 | 4  =>
       when others => raise ERROR;
    end case; 


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.

Rationale references: 2.2 Textual Structure, 3.7 Statements, 3.11 Case Statements,

Style Guide references: 3.3.7 Marker Comments, 5.6.1 Nesting, 5.6.3 Case Statements


[Ada Information Clearinghouse]

Address any questions or comments to