### 5.7.3.2 Examples of IF Constructs

The following example shows the simplest IF construct, an IF THEN statement with an END IF statement:

      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 one statement block, which consists of all the statements between the IF THEN and the END IF statements.

The IF THEN statement first evaluates the logical expression e. If the value of e is true, the statement block is executed. If the value of e is false, control transfers to the next executable statement after the END IF statement, and the block is not executed.

The following example contains an IF construct with 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

Block1 consists of all the statements between the IF THEN and the ELSE IF THEN statements. Block2 consists of all the statements between the ELSE IF THEN and the END IF statements.

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 contains an IF construct with 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 final example contains an IF construct with 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

There are four statement blocks in this example, consisting of the following:

- Block 1 consists of all the statements between the IF THEN and the first ELSE IF THEN statement.

- Block 2 consists of all the statements between the first ELSE IF THEN and the second ELSE IF THEN statement.

- Block 3 consists of all the statements between the second ELSE IF THEN and the ELSE statement.

- Block 4 consists of all the statements between the ELSE and END IF statement.

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.

Previous Page Next Page Table of Contents