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.
Syntax,
[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 ELSE CALL SUB3 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
.