EXIT
Purpose
The EXIT statement terminates execution of a
DO,
DO CONCURRENT
, or
DO WHILE construct before the construct terminates all of its
iterations. In addition, it can be used to terminate execution of a specified
construct that is not DO,
DO CONCURRENT
, or DO WHILE.
Syntax
Rules
If construct_name is specified, the EXIT statement must be within the construct specified by construct_name. If construct_name is not specified, the EXIT statement must be within the range of at least one DO or DO WHILE construct.
If construct_name is specified, the EXIT
statement belongs to the construct specified by construct_name. If
construct_name is not specified, the EXIT
statement belongs to the DO,
DO
CONCURRENT
, or DO WHILE construct that immediately surrounds
it.
An
EXIT statement must not be placed within a
CRITICAL or DO CONCURRENT construct if it
belongs to that construct or an outer construct.
If an EXIT statement belongs to a DO,
DO CONCURRENT
, or
DO WHILE construct, execution of the EXIT
statement causes the construct to become inactive. If the EXIT
statement is nested in any other DO,
DO
CONCURRENT
, or DO WHILE constructs, they also become
inactive. Any DO variable present retains its last defined value. If the
DO construct has no construct control, it will iterate infinitely unless
it becomes inactive. The EXIT statement can be used to make the construct
inactive.
If an EXIT statement belongs to a construct that is not
DO,
DO CONCURRENT
, or
DO WHILE, execution of the EXIT statement
terminates execution of the construct. Any DO,
DO
CONCURRENT
, or DO WHILE loops contained within the construct
become inactive.
An EXIT statement can have a statement label; it cannot be used as the labeled statement that terminates a construct.
Examples
LOOP1: DO I = 1, 20
N = N + 1
10 IF (N > NMAX) EXIT LOOP1 ! EXIT from LOOP1
LOOP2: DO WHILE (K==1)
KMAX = KMAX - 1
20 IF (K > KMAX) EXIT ! EXIT from LOOP2
END DO LOOP2
LOOP3: DO J = 1, 10
N = N + 1
30 IF (N > NMAX) EXIT LOOP1 ! EXIT from LOOP1
EXIT LOOP3 ! EXIT from LOOP3
END DO LOOP3
END DO LOOP1

a : BLOCK
DO i = 1, num_in_set
IF (X == a(i)) EXIT a ! EXIT from the a BLOCK construct
END DO
CALL r
END BLOCK a 
