Intel® Fortran Compiler 16.0 User and Reference Guide

IF Construct

Statement: 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.)

[name:] IF (expr) THEN

    block

[ELSE IF (expr) THEN [name]

   block]

[ELSE [name]

   block]

END IF [name]

name

(Optional) Is the name of the IF construct.

expr

Is a scalar logical expression enclosed in parentheses.

block

Is a sequence of zero or more statements or constructs.

Description

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. If a construct name is specified on an ELSE IF or ELSE statement, the same name must appear in the corresponding IF THEN and END IF statements.

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.

Note

No additional statement can be placed after the IF THEN statement in a block IF construct. For example, the following statement is invalid in the block IF construct:

   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.

The following figure shows the flow of control in IF constructs:

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.

Example

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 shows another example:

!  Simple block IF:
   IF (i .LT. 10) THEN
      ! the next two statements are only executed if i < 10
      j = i
      slice = TAN (angle)
   END IF

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

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 shows another example:

!  Block IF with ELSE IF statements:

   IF (j .GT. 1000) THEN
      ! Statements here are executed only if J > 1000
   ELSE IF (j .GT. 100) THEN
      ! Statements here are executed only if J > 100 and j <= 1000
   ELSE IF (j .GT. 10) THEN
      ! Statements here are executed only if J > 10 and j <= 100
   ELSE
      ! Statements here are executed only if j <= 10
   END IF

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 shows another example:

!  Nesting of constructs and use of an ELSE statement following
!  a block IF without intervening ELSE IF statements:
   IF (i .LT. 100) THEN
      ! Statements here executed only if i < 100
      IF (j .LT. 10) THEN
      ! Statements here executed only if i < 100 and j < 10
      END IF
      ! Statements here executed only if i < 100
   ELSE
      ! Statements here executed only if i >= 100
      IF (j .LT. 10) THEN
      ! Statements here executed only if i >= 100 and j < 10
      END IF
      ! Statements here executed only if i >= 100
   END IF

See Also