[Home] [Prev] [Next] [Index]
5.4 Case Statements
5.4 Case Statements
- 1
- 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.
- 2
- case_statement ::=
case
expression is
case_statement_alternative
{case_statement_alternative}
end case
;
case_statement_alternative ::=
when
choice {| choice } =>
sequence_of_statements
- 3
- 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.
- 4
- 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.
- 5
- 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.
- 6
- The execution of a case statement consists of the evaluation of the expression followed by the execution of the chosen sequence of statements.
- 7
- 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:
- 8
- 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.
- 9
- 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.
- 10
- References:

- base type 3.3

- choice 3.7.3

- context of overload resolution 8.7

- discrete type 3.5

- expression 4.4

- function call 6.4

- generic formal type 12.1

- conversion 4.6

- discrete type 3.5

- enumeration literal 3.5.1

- expression 4.4

- name 4.1

- object 3.2.1

- overloading 6.6

- overloading 8.7

- qualified expression 4.7

- sequence of statements 5.1

- static discrete range 4.9

- static subtype 4.9

- subtype 3.3

- type 3.3

- type conversion 4.6

- type mark 3.3.2
[Home] [Prev] [Next] [Index]
documentation@rational.com
Copyright © 1993-2000, Rational Software Corporation. All rights
reserved.