| Fortran Language Reference Manual, Volume 1 - S-3692-51 | ||
|---|---|---|
| Prev Section | Chapter 8. Controlling Execution | Next Section |
An IF construct selects at most one block of statements and constructs within the construct for execution. The IF statement controls the execution of only one statement; in previous Fortran standards it was called the logical IF statement. The arithmetic IF statement is not the same as the IF statement; it is a branching statement that is designated as obsolescent.
The IF construct contains one or more executable blocks. At most one block is executed. It is possible for no block to be executed when there is no ELSE statement.
The IF construct is defined as follows:
Table 8-2.
| if_construct | is |
| |
| if_then_stmt | is |
| |
| else_if_stmt | is |
| |
| else_stmt | is |
| |
| end_if_stmt | is |
|
Branching to an ELSE IF or an ELSE statement is prohibited.
Branching to an END IF statement is allowed from any block within the IF construct.
If a construct name appears on the IF-THEN statement, the same name must appear on the corresponding END IF statement.
The construct names on the ELSE IF and ELSE statements are optional, but if present must be the same name as the one on the IF-THEN statement. If one such ELSE IF or ELSE statement has a construct name, the others are not required to have a construct name.
The same construct name must not be used for different named constructs in the same scoping unit; thus, two IF constructs must not be both named INNER in the same executable part, for example.
The logical expressions are evaluated in order until one is found to be true. The block following the first true condition is executed, and the execution of the IF construct terminates. Subsequent true conditions in the construct have no effect. There may be no logical expressions found to be true in the construct. In this case, the block following the ELSE statement is executed if there is one; otherwise, no block in the construct is executed.
Figure 8-1 indicates the execution flow for an IF construct.
Example:
IF (I < J) THEN X = Y + 5.0 ELSE IF (I > 100) THEN X = 0.0 Y = -1.0 ELSE X = -1.0 Y = 0.0 END IF |
If I is less than J, the statement X = Y + 5.0 is executed and execution proceeds following the END IF statement. If I. J and if I > 100, the two statements following the ELSE IF statement are executed and execution proceeds following the END IF statement. If neither of these conditions is true, the block after the ELSE statement is executed.
The IF statement controls a single action statement, as opposed to a block of statements.
The scalar logical expression is evaluated. If true, the action statement is executed. If false, the action statement is not executed, and control passes to the next statement in the program.
The action statement must not be an IF statement or an END statement for a program, function, or subroutine. Note that the action statement cannot be any of the other END statements, such as END DO because they are not action statements.
If the logical expression contains a function reference, its evaluation may have side effects that modify the action statement.
A complete list of the action statements can be found in Section 2.5. Action statements change the definition state of variables or the condition of the I/O system, or are control statements. Specification statements such as type declaration statements, FORMAT statements, and ENTRY statements are not action statements. Note that constructs are not action statements.
| Prev Section | Table of Contents | Title Page | Index | Next Section |
| Blocks and Executable Constructs | Up one level | CASE Construct |