# SELECT RANK

Statement: Marks the beginning of a SELECT RANK construct. The construct selects for execution at most one of its constituent blocks. The selection is based on the rank of an assumed-rank variable.

[name:] SELECT RANK ([ assoc-name => ] selector)

[rank-case-stmt

block]...

END SELECT [name]

name | (Optional) Is the name of the SELECT RANK construct.
assoc-name | (Optional) Is an identifier that becomes associated with the selector. It becomes the associating entity. The identifier name must be unique within the construct. If unspecified, the associate name for the construct is selector.
selector | Is the name of an assumed-rank array.
rank-case-stmt | (Optional) Is one of the following: RANK (scalar-int-const-expr) [name] RANK (*) [name] RANK DEFAULT [name] The scalar-int-const-expr must be non-negative. The same rank value can only be specified in one rank-case-stmt. RANK DEFAULT and RANK (*) can be specified only once in the construct. RANK (*) cannot be specified if selector has the ALLOCATABLE or POINTER attribute.
block | (Optional) Is a sequence of zero or more statements or constructs.

## Description

If a construct name is specified at the beginning of a SELECT RANK statement, the same name must appear in the corresponding END SELECT statement. If a rank-case-stmt specifies a construct name, the corresponding SELECT RANK statement must specify the same name. The same construct name must not be used for different named constructs in the same scoping unit. If no name is specified at the beginning of a SELECT RANK statement, you cannot specify one following the END SELECT statement or any corresponding rank-case-stmt.

If no other rank-case-stmt of the construct matches the selector, a RANK DEFAULT statement, if present, identifies the block of code to be executed.

A SELECT RANK construct selects at most one block to be executed. During execution of that block, the associate name identifies an entity that is associated with the selector.

The following steps determine which block is selected for execution:

1. If the selector is argument associated with an assumed-size array, a RANK (*) statement identifies the block to be executed.

2. If the selector is not argument associated with an assumed-size array, a RANK (scalar-int-const-exp) statement identifies the block to be executed if the selector has that rank.

3. Otherwise, if there is a RANK DEFAULT statement, the block following that statement is executed.

A branch is allowed from within a block of a SELECT RANK construct to the END SELECT statement. Branches to the END SELECT from outside the construct are not allowed.

The associating entity in the block following a RANK DEFAULT statement is assumed rank and has the same attributes as the selector; it can used only in contexts that an assumed-rank entity can be used. The associating entity in the block following a RANK (*) statement is an assumed-size one dimensional array and lower bound 1, as if declared DIMENSION(1:*).

The associating entity in the block following a RANK (scalar-int-const-exp) statement has the rank specified. The lower bound is the lower bound of the selector for each corresponding dimension, and the upper bound is the upper bound of the selector for each corresponding dimension of the selector. The associating entity in a RANK(*) or a RANK (scalar-int-const-exp) block is a variable and may appear in variable definition contexts.

If the selector has the POINTER, TARGET or ALLOCATABLE attribute, the associating entity has the same attributes.

## Example

The following example shows a SELECT RANK construct that initializes scalars, rank 1, and rank 2 arrays to zero. If the dummy argument rank is greater than 2, an error message is printed.

    SUBROUTINE INITIALIZE (ARG)
      REAL :: ARG(..)
      SELECT RANK (ARG)
        RANK (0)   ! Scalar
          ARG = 0.0
        RANK (1)
          ARG(:) = 0.0
        RANK (2)
          ARG(:, :) = 0.0
        RANK DEFAULT
          PRINT *, "Subroutine initialize called with unexpected rank argument"
      END SELECT
      RETURN
    END SUBROUTINE

The following example shows how to use a select rank to initialize assumed-size arrays of any rank to zero:

    SUBROUTINE INITIALIZE (ARG, SIZE)
      REAL,CONTIGUOUS :: ARG(..)
      INTEGER         :: SIZE, I
      SELECT RANK (ARG)
        RANK (0)   ! Special case the scalar case
          ARG = 0.0
        RANK (*)
          DO I = 1, SIZE
            ARG(I) = 0.0
          END DO
      END SELECT
      RETURN
    END SUBROUTINE

Parent topic: S

## See Also

Construct Association