FORTRAN 77 Language Reference

Description

The block IF statement evaluates a logical expression and, if the logical expression is true, it executes a set of statements called the IF block. If the logical expression is false, control transfers to the next ELSE, ELSE IF, or END IF statement at the same IF-level.

IF Level

The IF level of a statement S is the value n1-n2, where n1 is the number of block IF statements from the beginning of the program unit up to the end, including S; n2 is the number of END IF statements in the program unit up to, but not including, S.

Example: In the following program, the IF-level of statement 9 is 2-1, or, 1


       IF ( X .LT. 0.0 ) THEN 
              MIN = NODE 
       END IF 
       ... 
9      IF ( Y .LT. 0.0 ) THEN 
              MIN = NODE - 1 
       END IF 

:

The IF-level of every statement must be zero or positive. The IF-level of each block IF, ELSE IF, ELSE, and END IF statement must be positive. The IF-level of the END statement of each program unit must be zero.

IF Block

An IF block consists of all the executable statements following the block IF statement, up to, but not including, the next ELSE, ELSE IF, or END IF statement that has the same if level as the block IF statement. An IF block can be empty. In the following example, the two assignment statements form an IF block:


       IF ( X .LT. Y ) THEN 
              M = 0 
              N = N+1 
       END IF

Execution proceeds as follows:

  1. The logical expression e is evaluated first. If e is true, execution continues with the first statement of the IF block.

  2. If e is true and the IF block is empty, control is transferred to the next END IF statement with the same IF level as the block IF statement.

  3. If e is false, control is transferred to the next ELSE IF, ELSE, or END IF statement with the same IF level as the block IF statement.

  4. If the last statement of the IF block does not result in a branch to a label, control is transferred to the next END IF statement that has the same IF level as the block IF statement preceding the IF block.