If and Other Decision Structures

FORTRAN provides several ways we can make decisions in our programs.


The If statement

There are several versions of the IF statement.

Block IF

The format for this is:

         IF (logical expression) THEN
           (one or more statements)
         ELSE
           (one or more statements)
         END IF

If the logical expression is .TRUE., the first block of statements is executed. If the expression is .FALSE., the second block of statements is executed. Either way, only one of the two blocks of statements will be executed, after which control jumps to the line after END IF.

If we do not need the ELSE case, we can omit it:

         IF (logical expression) THEN
           (one or more statements)
         END IF

The END If statement is a scope terminator and is needed if we have a block IF.

Examples:
          IF (Counter == 0) THEN
            PRINT *, ' Counter is 0 and we cannot compute the average.'
          ELSE
            Average = Total / Counter
            PRINT *, 'The average is ', Average
          END IF

          IF (Grand_Percent > 60.0) THEN
            PRINT *, 'Passing grade'
          END IF

Logical IF

If we do not have an ELSE case and there is only one statement to be executed in the THEN case, we can write this as one line:

         IF (logical expression) statement

This does not allow the use of ELSE and does not require or allow the use of THEN or END IF.

If the logical expression is .FALSE., the statement is not executed, and control continues with the statement immediately following the IF.

Nested IF constructs

IF constructs can be nested, as in:

         IF (logical expression) THEN
           IF (logical expression) THEN
             (one or more statements)
           ELSE
             (one or more statements)
         ELSE          
           (one or more statements)
         ENDIF
Example:  
          IF (Sex == 'M')
            IF (Age < 30)
              YM-Count = YM-Count + 1
            ELSE
              OM-Count = YO-Count + 1
            END IF    
          ELSE
            IF (Age < 30)
              YF-Count = YM-Count + 1
            ELSE
              OF-Count = YO-Count + 1
            END IF
          END IF

We can also use ELSE IF, as in:

         IF (logical expression) THEN
           (one or more statements)
         ELSE IF (logical expression) THEN
           (one or more statements)
         ELSE
           (one or more statements)
         END IF

Notice we have only one END IF here.

We can nest IF constructs up to 125 levels deep.

Case structure

Sometimes we want to make a choice from a list of equally important possibilities. One way to do this is with an ELSE IF structure. Another way, introduced in FORTRAN 90, is SELECT. It is used as follows:

         SELECT CASE (selector)
           CASE (value-list)
             (one or more statements)
           CASE (value-list)
             (one or more statements)
           CASE DEFAULT
             (one or more statements)
         END SELECT

where "selector" is an integer, character or logical expression (in particular, a variable), and each "value-list" is a list of one or more possible values of the selector, or the term DEFAULT.

If the selector does not match any of the cases, the block of code in the DEFAULT case will be executed. We could have many cases, or just a few.

Example:
          SELECT CASE (NUM)
            CASE (1:3)
              PRINT *, 'NUM is 1, 2 or 3'
            CASE (4)
              PRINT *, 'NUM is 4'
            CASE DEFAULT
              PRINT *, 'NUM is not in the range 1 to 4'
          END SELECT

Here (1:3) indicates values from 1 to 3 inclusive. If the list contained (5:), this would indicate 5 or larger; if it contained (:17), this would indicate 17 or less. The DEFAULT case is not mandatory but including it is probably a good practice.


A comment about style

With any of these structures, it is advisable to use indentation to make the layout of the code on the page reflect the logical structure. If we line everything up in the same column, it can be difficult to see which ELSE matches which IF, etc.


Old-style FORTRAN

In older versions of FORTRAN, there were other control structures available. Nowadays it is not considered advisable to use these.

These all involve situations in which we have labels (numbers of up to 5 digits on executable statements in various parts of our code.

Unconditional GO TO statement

The format for this is simply:

          GO TO label

Control jumps to the statement with the given label.

Computed GO TO

The format for this is a bit fancier:

          GO TO label1, label2, ... labeln, expression

Here the expression must have an INTEGER value matching one of the labels (which are, after all, just integer values), and control jumps to the statement with the matching label.

Arithmetic IF

Suppose we have labels (numbers of up to 5 digits) on statements in various parts of our code. An "arithmetic IF" allows us to shift control to one of these locations:

         IF (arithmetic expression) label1, label2, label3

The expression is evaluated and control branches to label1, label2 or label3, according to whether the value is negative, zero or positive, respectively. (The expression may be REAL or INTEGER but not COMPLEX.) It is possible for two of the three labels to be the same. (If all three are the same, we have a GO TO statement.)

A statement immediately following an arithmetic IF must have a label, as it cannot otherwise be reached in the flow of the program.