Decisions

The IF ... THEN Statement

Decisions in FORTRAN are accomplished with an IF-THEN program structure. Usually the block of code affected by the decision is indented to make it stand out from the rest of the program. The logical expression is something that is true or false. If it is true, the block of FORTRAN statements is carried out. If it is false, the program continues from the statement after the END IF.

     IF (logical expression) THEN
       block
     END IF

The most common form of logical expression takes the form:

(ArithmeticExpression RelationalOperator ArithmeticExpression)

Example

       (A .GT. 0)

The relational operators are: (note that the full stops must be on either side)

.GT. Greater than
.LT. Less than
.EQ. Equal to
.GE. Greater than or equal to
.LE. Less than or equal to
.NE. Not equal to

Logical expressions may be linked with the logical operators:

Note: When using .AND. or .OR. each logical expression must be bracketed and the entire logical expression must also be bracketed, as in the example below.

Example

    IF ((A.GT.B).AND.(B.LT.C)) THEN
    END IF
     
    IF ((C.GT.D).OR.(B.EQ.C)) THEN
    IF ((C.GT.D).OR.(B.EQ.C)) THEN
    END IF

Logical expressions may be preceded by .NOT. which reverses the expression's truth.

Example

     IF(.NOT.(A.GT.B)) THEN

     END IF

In English the above expression reads: if A is not greater than B.

IF ... THEN ... ELSE ...

This decision structure is used if code is to be executed whether the logical expression is true or false. In the example below, Block A is executed if the expression is true and Block B is executed if the expression is false.

    IF (logical expression) THEN
       block A
    ELSE
       block B
    END IF
7.3 IF ... THEN ... ELSE IF ... THEN ...

This structure is used when more than one block of code can be executed if the initial logical expression is false or if you need to make additional decisions. Because there are more blocks with the potential for execution, there need to be additional decisions to determine if the block can be executed or not. If none of the decisions are true, then none of the blocks are executed.

      IF (decision 1) THEN
         Block A
      ELSE IF (decision 2) THEN
         Block B
      ELSE IF (decision 3) THEN
         Block C
      END IF

IF ... THEN ELSE IF ... THEN ELSE

This is a third possible decision structure which can be used to make multiple decisions. If none of those decisions are true then Block D iS executed.
         IF (decision1) THEN
               Block A
         ELSE IF (decision2) THEN
               Block B
         ELSE IF (decision 3) THEN
               Block C
         ELSE
               Block D
         END IF

IF without THEN and END IF

There is an additional decision structure which may be useful. If the block of code you wish to execute is only one line and there are no additional decisions to be made, you can eliminate the THEN and the END IF statements. You cannot do something like this with ELSE and ELSE IF decision structures.

Example

IF (A .GT. B) PRINT*, 'B is greater than A'

IF (NUMBER .LT. MIN) MIN = NUMBER

Now go to the exercises and work your way through the checking variables exercise.


Work your way through the following components attempting the exercises as you come across them:

| Programs | Variables | Arithmetic Operations | Input and Output | Looping in Programs | Arrays in Programs | Checking variables | Subprograms and functions |