The IF construct conditionally executes one block of constructs or statements depending on the evaluation of a logical expression. (This construct was called a block IF statement in FORTRAN 77.)
The IF construct takes the following form:
If a construct name is specified at the beginning of an IF THEN statement, the same name must appear in the corresponding END IF statement. The same construct name must not be used for different named constructs in the same scoping unit.
Depending on the evaluation of the logical expression, one block or no block is executed. The logical expressions are evaluated in the order in which they appear, until a true value is found or an ELSE or END IF statement is encountered.
Once a true value is found or an ELSE statement is encountered, the block immediately following it is executed and the construct execution terminates.
If none of the logical expressions evaluate to true and no ELSE statement appears in the construct, no block in the construct is executed and the construct execution terminates.
IF (e) THEN I = J
This statement is translated as the following logical IF statement:
IF (e) THENI = J
You cannot use branching statements to transfer control to an ELSE IF statement or ELSE statement. However, you can branch to an END IF statement from within the IF construct.
Figure 7-4 shows the flow of control in IF constructs.
Figure 7-4 Flow of Control in IF Constructs
You can include an IF construct in the statement block of another IF construct, if the nested IF construct is completely contained within a statement block. It cannot overlap statement blocks.
The following example shows the simplest form of an IF construct:
Form Example
IF (expr) THEN IF (ABS(ADJU) .GE. 1.0E-6) THEN
block TOTERR = TOTERR + ABS(ADJU)
QUEST = ADJU/FNDVAL
END IF END IF
This construct conditionally executes the block of statements between the IF THEN and the END IF statements.
The following example shows an IF construct containing an ELSE statement:
Form Example
IF (expr) THEN IF (NAME .LT. 'N') THEN
block1 IFRONT = IFRONT + 1
FRLET(IFRONT) = NAME(1:2)
ELSE ELSE
block2 IBACK = IBACK + 1
END IF END IF
Block1 consists of all the statements between the IF THEN and ELSE statements. Block2 consists of all the statements between the ELSE and the END IF statements.
If the value of the character variable NAME is less than
'
N'
, block1 is executed. If the value
of NAME is greater than or equal to '
N'
,
block2 is executed.
The following example shows an IF construct containing an ELSE IF THEN statement:
Form Example
IF (expr) THEN IF (A .GT. B) THEN
block1 D = B
F = A - B
ELSE IF (expr) THEN ELSE IF (A .GT. B/2.) THEN
block2 D = B/2.
F = A - B/2.
END IF END IF
If A is greater than B, block1 is executed. If A is not greater than B, but A is greater than B/2, block2 is executed. If A is not greater than B and A is not greater than B/2, neither block1 nor block2 is executed. Control transfers directly to the next executable statement after the END IF statement.
The following example shows an IF construct containing several ELSE IF THEN statements and an ELSE statement:
Form Example
IF (expr) THEN IF (A .GT. B) THEN
block1 D = B
F = A - B
ELSE IF (expr) THEN ELSE IF (A .GT. C) THEN
block2 D = C
F = A - C
ELSE IF (expr) THEN ELSE IF (A .GT. Z) THEN
block3 D = Z
F = A - Z
ELSE ELSE
block4 D = 0.0
F = A
END IF END IF
If A is greater than B, block1 is executed. If A is not greater than B but is greater than C, block2 is executed. If A is not greater than B or C but is greater than Z, block3 is executed. If A is not greater than B, C, or Z, block4 is executed.
The following example shows a nested IF construct:
Form Example
IF (expr) THEN IF (A .LT. 100) THEN
block1 INRAN = INRAN + 1
IF (expr2) THEN IF (ABS(A-AVG) .LE. 5.) THEN
block1a INAVG = INAVG + 1
ELSE ELSE
block1b OUTAVG = OUTAVG + 1
END IF END IF
ELSE ELSE
block2 OUTRAN = OUTRAN + 1
END IF END IF
If A is less than 100, the code immediately following the IF is executed. This code contains a nested IF construct. If the absolute value of A minus AVG is less than or equal to 5, block1a is executed. If the absolute value of A minus AVG is greater than 5, block1b is executed.
If A is greater than or equal to 100, block2 is executed, and the nested IF construct (in block1) is not executed.
The following example shows a named IF construct:
BLOCK_A: IF (D> 0.0) THEN ! Initial statement for named construct
RADIANS = ACOS(D) ! These two statements
DEGREES = ACOSD(D) ! form a block
END IF BLOCK_A ! Terminal statement for named construct