next up previous contents
Next: Indexed DO Loop Up: Control Flow Previous: Conditional Cycle Loops

 

Named and Nested Loops

Sometimes it is necessary to jump out of more than the innermost DO loop. To allow this, loops can be given names and then the EXIT statement can be made to refer to a particular loop. An analogous situation also exists for CYCLE,

    outa: DO
     inna: DO
      ...
      IF (a.GT.b) EXIT outa
      ...
      IF (a.EQ.b) CYCLE outa
      ...
      IF (c.GT.d) EXIT inna
      ...
     END DO inna
     ...
    END DO outa

The (optional) name following the EXIT or CYCLE highlights which loop the statement refers to.

For example,

   IF (a.EQ.b) CYCLE outa

causes a jump to the first DO loop named outa.

Likewise,

   IF (c.GT.d) EXIT inna

jumps to the statement after

    END DO inna
If the name is missing then the directive is applied, as usual, to the next outermost loop.

The scope of a loop name is the same as that of any construct name.

Now try this question gif


next up previous contents
Next: Indexed DO Loop Up: Control Flow Previous: Conditional Cycle Loops

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 15:03:35 GMT 1996
Not for commercial use.