



IF-Blocks IF-block looks like this:
IF(N .NE. 0) THEN AVERAG = SUM / N AVGSQ = SUMSQ / N END IF |
The IF-block can also contain an ELSE statement to handle the alternative:
IF(B**2 .GE. 4.0 * A * C) THEN WRITE(UNIT=*,FMT=*)'Real roots' ELSE WRITE(UNIT=*,FMT=*)'No real roots' END IF |
IF statement contains a logical expression its value can only be true or
false, thus one or other of these blocks will always be executed.
If there are several alternative conditions to be tested, they can be specified with
ELSE IF statements:
IF(OPTION .EQ. 'PRINT') THEN
CALL OUTPUT(ARRAY)
ELSE IF(OPTION .EQ. 'READ') THEN
CALL INPUT(ARRAY)
ELSE IF(OPTION .EQ. 'QUIT') THEN
CLOSE(UNIT=OUT)
STOP 'end of program'
ELSE
WRITE(UNIT=*,FMT=*)'Incorrect reply, try again...'
END IF
|
The general form of the block-if structure is as follows:
IF( logical-expression ) THEN a block of statements ELSE IF( logical-expression ) THEN another block of statements ELSE a final block of statements END IF |
IF THEN, ELSE IF, and ELSE statements each govern one block of statements.
There can be any number of ELSE IF statements. The ELSE statement (together with
its block) is also optional, and there can be at most one of these.
The first block of statements is executed only if the first expression is true.
Each block after an ELSE IF is executed only if none of the preceding blocks
have been executed and the attached ELSE IF expression is true. If there is
an ELSE block it is executed only if none of the preceding blocks has been
executed.
After a block has been executed control is transferred to the statement following
the END IF statement at the end of the structure (unless the block ends with some
statement which transfers control elsewhere).
Any block can contain a complete block-IF structure properly nested
within it, or a complete DO-loop, or any other executable statements (except
END).
It is illegal to transfer control into any block from outside it, but there is no restriction on transferring control out of a block.
The rules for logical expressions are covered in section 7.7.
The indentation scheme shown in the examples above is not mandatory but the
practice of indenting each block by a few characters relative to the rest of the
program is strongly recommended. It makes the structure of the block immediately
apparent and reduces the risk of failing to match each IF with an END IF. An
indenting scheme is especially useful when IF-blocks are nested within others. For
example:
IF(POWER .GT. LIMIT) THEN
IF(.NOT. WARNED) THEN
CALL SET('WARNING')
WARNED = .TRUE.
ELSE
CALL SET('ALARM')
END IF
END IF
|
IF-blocks are
nested to a very great depth: but this tends to mean that the program unit is
getting too complicated and that it will usually be beneficial to divide it into
subroutines. If you accidentally omit an END IF statement the compiler will
flag the error but will not know where you forgot to put it. In such cases
the compiler may get confused and generate a large number of other error
messages.
When an IF-block which is executed frequently contains a large number of ELSE
IF statements it will be slightly more efficient to put the most-likely conditions near
the top of the list as when they occur the tests lower down in the list will not need to
be executed.