FORTRAN 77 Language Reference

ELSE IF

The ELSE IF provides a multiple alternative decision structure.

ELSE IF (e2) THEN

IF (e1) THEN

END IF

where e1 and e2 are logical expressions.

Description

You can make a series of independent tests, and each test can have its own sequence of statements.

An ELSE IF block consists of all the executable statements following the ELSE IF statement up to, but not including, the next ELSE IF, ELSE, or END IF statement at the same IF level as the ELSE IF statement.

An ELSE IF block can be empty.

Execution of the ELSE IF (e) proceeds as follows, depending on the value of the logical expression, e:

  1. e is evaluated.

  2. If e is true, execution continues with the first statement of the ELSE IF block. If e is true and the ELSE IF block is empty, control is transferred to the next END IF statement at the same IF level as the ELSE IF statement.

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

Restrictions

You cannot jump into an ELSE IF block from outside the ELSE IF block.

The statement label, if any, of an ELSE IF statement cannot be referenced by any statement.

A matching END IF statement of the same IF level as the ELSE IF must appear before any ELSE IF or ELSE statement at the same IF level.

Example

Example: ELSE IF:


       READ (*,*) N 
       IF ( N .LT. 0 ) THEN 
              WRITE(*,*) 'N<0'
       ELSE IF ( N .EQ. 0) THEN
              WRITE(*,*) 'N=0' 
       ELSE
              WRITE(*,*) 'N>0'
       END IF