# EXIT Statement

Statement: Terminates execution of a DO loop or a named construct.

EXIT [name]

name | (Optional) Is the name of the DO loop or construct.

## Description

The EXIT statement causes execution of a DO loop or a named construct to be terminated.

If name is specified, the EXIT statement must be within the range of that named construct. Otherwise, the EXIT statement must be within a DO loop and it exits the innermost DO within which it appears.

If a DO loop is terminated, any inner DO loops are also terminated and the DO control variables retain their last value. If a non-DO construct is terminated, any DO loops inside that construct are also terminated.

An EXIT statement must not appear within a DO CONCURRENT construct if it belongs to that construct or an outer construct.

An EXIT statement must not appear in a CHANGE TEAM or CRITICAL construct if it belongs to an outer construct.

An EXIT statement can appear in any of the following constructs:

- ASSOCIATE

- BLOCK

- CHANGE TEAM

- CRITICAL

- DO

- IF

- SELECT CASE

- SELECT RANK

- SELECT TYPE

An EXIT statement cannot appear in a WHERE or FORALL construct.

## Example

The following examples demonstrate EXIT statements.

Example 1:

    LOOP_A : DO I = 1, 15
      N = N + 1
      IF (N > I) EXIT LOOP_A
    END DO LOOP_A

Example 2:

        INTEGER numpoints, point
        REAL datarray(1000), sum
        sum = 0.0
        DO point = 1, 1000
          sum = sum + datarray(point)
          IF (datarray(point+1) .EQ. 0.0) EXIT
        END DO

Example 3:

    DO I=1,N
       MyBlock:  BLOCK
        REAL :: T
        T = A(I) + B(I)
        IF (T == 0.0) EXIT MyBlock
        C(I) = T + SQRT(T)
        END BLOCK
    END DO

Example 4:

    DO CONCURRENT (I = 1:N)
       MyBlock:  BLOCK
         REAL :: T
        T = A(I) + B(I)
        IF (T == 0.0) EXIT MyBlock
        C(I) = T + SQRT(T)
        END BLOCK
    END DO

The following example shows illegal EXIT statements in DO CONCURRENT and CRITICAL:

    LOOP_1 : DO CONCURRENT (I = 1:N)
      N = N + 1
      IF (N > I) EXIT LOOP_1   ! can’t EXIT DO CONCURRENT
    END DO LOOP_1

    LOOP_2 : DO I = 1, 15
      CRITICAL
        N = N + 1
        IF (N > I) EXIT LOOP_2      ! can’t EXIT outer construct from inside
      END CRITICAL                  !   CHANGE TEAM, DO CONCURRENT, or CRITICAL
    END DO LOOP_2

Parent topic: E to F

## See Also

DO
DO WHILE