XL Fortran for AIX 8.1

Language Reference


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---------------------------------------><
 
 
>>-+--------------------------+--------------------------------><
   | .----------------------. |
   | V                      | |
   '---CASE_statement_block-+-'
 
 
>>-END_SELECT_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----------------------------------------------><
 
 
>>-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 construct executes as follows:

  1. The case expression is evaluated. The resulting value is the case index.
  2. The case index is compared to the case_selector of each CASE statement.
  3. If a match occurs, the statement block associated with that CASE statement is executed. No statement block is executed if no match occurs. (See CASE.)
  4. 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.

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

Examples

      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


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]