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--------------------------------------->< >>-+--------------------------+-------------------------------->< | .----------------------. | | V | | '---CASE_statement_block-+-' >>-END_SELECT_statement---------------------------------------->< |
>>-CASE_statement---------------------------------------------->< >>-statement_block--------------------------------------------->< |
In the construct, each case value must be of the same type as the case expression.
The CASE construct executes as follows:
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. FORTRAN 77 source 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 Fortran 90 or Fortran 95 source SELECTCASE(I) CASE(3) CALL SUBA() CASE(5) CALL SUBB() CASE(6) CALL SUBC() CASE DEFAULT CALL OTHERSUB() END SELECT END |
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