next up previous contents
Next: Conditional Exit Loops Up: Control Flow Previous: IF Construct

 

Nested and Named IF Constructs

All control constructs can be both named and nested, for example,

    outa: IF (a .NE. 0) THEN
            PRINT*, "a /= 0"
            IF (c .NE. 0) THEN
              PRINT*, "a /= 0 AND c /= 0"
            ELSE
              PRINT*, "a /= 0 BUT c == 0"
            ENDIF
          ELSEIF (a .GT. 0) THEN outa
            PRINT*, "a > 0"
          ELSE outa
            PRINT*, "a must be < 0"
          ENDIF outa

Here the names are only cosmetic and are intended to make the code clearer (cf DO-loop names which do). If a name is given to the IF statement then it must be present on the ENDIF statement but not necessarily on the ELSE or ELSEIF statement. If a name is present on the ELSE or ELSEIF then it must be present on the IF statement.

The example has two nested and one named IF block. Nesting can be to any depth (unless the compiler prohibits this, even if it does the limit will almost certainly be configurable).

Even though construct names are only valid within the block that they apply to, their scope is the whole program unit. This means that a name may only be used once in a scoping unit even though no confusion would arise if it were re-used. (See Section 13.8 for a discussion of scope.)

Now try this question gif

Return to corresponding overview page gif


next up previous contents
Next: Conditional Exit Loops Up: Control Flow Previous: IF Construct

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.