|Fortran Language Reference Manual, Volume 1 - S-3692-51|
|Prev Section||Chapter 8. Controlling Execution||Next Section|
The CASE construct, like the IF construct, consists of a number of blocks, of which at most one is selected for execution. The selection is based on the value of the scalar expression in the SELECT CASE statement at the beginning of the construct; the value of this expression is called the case index. The case selected is the one for which the case index matches a case selector value in a CASE statement. There is an optional default case that, in effect, matches all values not matched by any other CASE statement in the construct.
[ case_construct_name : ] SELECT CASE (case_expression) [ CASE (case_value_range_list) [ case_construct_name ] block ] ... [ CASE DEFAULT [ case_construct_name ] block ] END SELECT [ case_construct_name ]
The case construct is defined as follows:
The statement containing the keywords SELECT CASE is called the SELECT CASE statement. The statement beginning with the keyword CASE is called the CASE statement. The statement beginning with the keywords END SELECT is called the END SELECT statement. A case value range list enclosed in parenthesis or the DEFAULT keyword is called a case selector.
If a construct name is present on a SELECT CASE statement, it must also appear on the END SELECT statement.
Any of the case selector statements may or may not have a construct name. If one does, it must be the same name as the construct name on the SELECT CASE statement.
A CASE statement with the case selector DEFAULT is optional. If it is present, it is not required to be the last CASE statement.
The case_expr must be a scalar expression of type integer, character, or logical.
Within a particular CASE construct, the case expression and all case values must be of the same type and must have the same kind type parameter values. If the character type is used, different character lengths are allowed.
Each case_value must be a scalar initialization expression of the same type as the case expression. An initialization expression is an expression that can be evaluated at compile time; that is, essentially, a constant expression.
The colon forms of the case values expressing a range can be used for expressions in the construct of type integer and character but not type logical. For example, the following CASE statement would select all character strings that collate between BOOK and DOG, inclusive:
After expression evaluation, there must be no more than one case selector that matches the case index. In other words, overlapping case values and case ranges are prohibited.
Branching to the END SELECT statement is allowed only from within the construct. Branching to a CASE statement is prohibited; branching to the SELECT CASE statement is allowed, however.
The following example shows the CASE construct:
! Compute the area with a formula appropriate for ! the shape of the object FIND_AREA: & SELECT CASE (OBJECT) CASE (CIRCLE) AREA = PI * RADIUS ** 2 CASE (SQUARE) AREA = SIDE * SIDE CASE (RECTANGLE) AREA = LENGTH * WIDTH CASE DEFAULT PRINT*, "Unable to compute area." END SELECT FIND_AREA
The case index (the scalar expression) in the SELECT CASE statement is evaluated in anticipation of matching one of the case values preceding the blocks. The case index must match at most one of the selector values. The block following the case matched is executed, the CASE construct terminates, and control passes to the next executable statement or construct following the END SELECT statement of the construct. If no match occurs and the CASE DEFAULT statement is present, the block after the CASE DEFAULT statement is selected. If there is no CASE DEFAULT statement, the CASE construct terminates, and the next executable statement or construct following the END SELECT statement of the construct is executed. If the case value is a single value, a match occurs if the index is equal to the case value (determined by the rules used in evaluating the equality or equivalence operator). If the case value is a range of values, there are three possibilities to determine a match depending on the form of the range:
Case value range
Condition for a match
case_value1 : case_value2
case_value1 < case_index < case_value2
case_value < case_index
case_value > case_index
Figure 8-2 illustrates the execution of a CASE construct.
INDEX = 2 SELECT CASE (INDEX) CASE (1) X = 1.0 CASE (2) X = 2.0 CASE DEFAULT X = 99.0 END SELECT
The case expression INDEX has the value 2. The block following the case value of 2 is executed; that is, the statement X = 2.0 is executed, and execution of the CASE construct terminates.
COLOR = 'GREEN' SELECT CASE (COLOR) CASE ('RED') STOP CASE ('YELLOW') CALL PROCEED_IF_YOU_CAN_SAFELY CASE ('GREEN') CALL GO_AHEAD END SELECT
This example uses selectors of type character. The expression COLOR has the value GREEN, and therefore the procedure GO_AHEAD is executed. When it returns, the execution of the CASE statement terminates, and the executable statement after the END SELECT statement executes next.