next up previous contents
Next: Nested and Named IF Up: Control Flow Previous: IF Statement

 

IF Construct

The block-IF is more flexible than the single statement IF since there can be a number of alternative mutually exclusive branches guarded by (different) predicates. The control flow is a lot more structured than if a single statement IF plus GOTO statements were used. The scenario is that the predicates in the IF or ELSEIF lines are tested in turn, the first one which evaluates as true transfers control to the appropriate inner block of code; when this has been completed control passes to the ENDIF statement and thence out of the block. If none of the predicates are true then the < else-block > (if present) is executed.

The simplest form of the IF construct is equivalent to the IF\ statement, in other words, if a predicate is .TRUE. then an action is performed.

Consider the IF ... THEN construct

    IF (I > 17) THEN
      Print*, "I > 17"
    END IF

this maps onto the following control flow structure,

  figure4556
Figure 2: Visualisation of an IF ... THEN Construct

It is a matter of personal taste whether the above construct or just the simple IF statement is used for this sort of case.

The IF construct may contain an ELSE branch, a simple example could be,

    IF (I > 17) THEN
      Print*, "I > 17"
    ELSE
      Print*, "I <= 17"
    END IF

this maps onto the following control flow structure,

  figure4562
Figure 3: Visualisation of an IF ... THEN ... ELSE Construct

Can also have an ELSEIF branch:

    IF (I > 17) THEN
      Print*, "I > 17"
    ELSEIF (I == 17)
      Print*, "I == 17"
    ELSE
      Print*, "I < 17"
    END IF

Both ELSE and ELSEIF are optional and there can be any number of ELSEIF branches. The above maps to the following control flow structure

  figure4571
Figure 4: Visualisation of an IF ... THEN ... ELSEIF Construct

The formal syntax is,

 
[< name >:]IF(< logical-expression >)THEN

< then-block >

[ ELSEIF(< logical-expression >)THEN [< name >]

< elseif-block >

... ]

[ ELSE [< name >]

< else-block > ]

END IF [< name >]

The first branch to have a .TRUE. valued < logical-expression > is the one that is executed. If none are found then the < else-block >, if present, is executed.

For example,

    IF (x .GT. 3) THEN
     CALL SUB1
    ELSEIF (x .EQ. 3) THEN
     CALL SUB2
    ELSEIF (x .EQ. 2) THEN
     CALL SUB3
    ELSE
     CALL SUB4
    ENDIF

(A further IF construct may appear in the < then-block >, the < else-block > or the < elseif-block >. This is now a nested IF structure.)

Statements in either the < then-block >, the < else-block > or the < elseif-block > may be labelled but jumps to such labelled statements are permitted only from within the block containing them. Entry into a block-IF construct is allowed only via the initial IF statement. Transfer out of either the < then-block >, the < else-block > or the < elseif-block > is permitted but only to a statement entirely outside of the whole block defined by the IF...END IF statements. A transfer within the same block-IF between any of the blocks is not permitted.

Certain types of statement, eg, END SUBROUTINE, END FUNCTION or END PROGRAM, statement are not allowed in the < then-block >, the < else-block > or the < elseif-block >.

Return to corresponding overview page gif


next up previous contents
Next: Nested and Named IF Up: Control Flow Previous: IF Statement

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