CASE construct
The CASE construct has a concise syntax for selecting, at most, one of a number of statement blocks for execution. The case selector of each CASE statement is compared to the expression of the SELECT CASE statement.
- SELECT_CASE_statement
- defines the case expression that is to be evaluated. See SELECT CASE for syntax details.
- END_SELECT_statement
- terminates the CASE construct. See END (Construct) for syntax details.
- CASE_statement_block
- CASE_statement
- defines the case selector, which is a value, set of values, or default case, for which the subsequent statement block is executed. See CASE for syntax details.
In the construct, each case value must be of the same type as the case expression.
- The case expression is evaluated. The resulting value is the case index.
- The case index is compared to the case_selector of each
CASE statement.
- If a match occurs, the statement block associated with that CASE statement is executed.
- If no match occurs, but a default case_selector exists, the statement block associated with the default case_selector is executed.
- Otherwise, no statement block is executed.
- Execution of the construct is complete and control is transferred to the statement after the END SELECT statement.
A CASE construct contains zero or more CASE statements that can each specify a value range, although the value ranges specified by the CASE statements cannot overlap.
A default case_selector can be specified by one of the CASE statements. A default CASE_statement_block can appear anywhere in the CASE construct; it can appear at the beginning or end, or among the other blocks.
If a construct name is specified, it must appear on the SELECT CASE statement and END SELECT statement, and optionally on any CASE statements.
You can only branch to the END SELECT statement from within the CASE construct. A CASE statement cannot be a branch target.
Migration Tip:
Use CASE in place of block IFs.
IF (I .EQ.3) THEN
CALL SUBA()
ELSE IF (I.EQ. 5) THEN
CALL SUBB()
ELSE IF (I .EQ. 6) THEN
CALL SUBC()
ELSE
CALL OTHERSUB()
ENDIF
END SELECTCASE(I)
CASE(3)
CALL SUBA()
CASE(5)
CALL SUBB()
CASE(6)
CALL SUBC()
CASE DEFAULT
CALL OTHERSUB()
END SELECT
ENDExamples
ZERO: SELECT CASE(N)
CASE DEFAULT ZERO
OTHER: SELECT CASE(N) ! start of CASE construct OTHER
CASE(:-1)
SIGNUM = -1 ! this statement executed when n≤-1
CASE(1:) OTHER
SIGNUM = 1
END SELECT OTHER ! end of CASE construct OTHER
CASE (0)
SIGNUM = 0
END SELECT ZERO
END
