The EXIT statement terminates execution of a DO construct or DO WHILE construct before the construct completes all of its iterations.
The EXIT statement is placed within a DO or DO WHILE construct and belongs to the DO or DO WHILE construct specified by DO_construct_name or, if not specified, by the DO or DO WHILE construct that immediately surrounds it. When a DO_construct_name is specified, the EXIT statement must be in the range of that construct.
When the EXIT statement is executed, the DO or DO WHILE construct that the EXIT statement belongs to becomes inactive. If the EXIT statement is nested in any other DO 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.
An EXIT statement can have a statement label; it cannot be used as the labeled statement that terminates a DO or DO WHILE construct.
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