« previous next»

3.1    Branches

    Another important technique when writing Fortran code is the ability to select different paths of execution.  Your code may need to branch depending on different criteria.  The most important of these are:


IF

    The simplest form is the logical IF statement.  In general:

    IF (logical -criteria) execution statement

If the logical-criteria is TRUE then the execution is performed.  If not the execution statement is bypassed.  An example:

    IF (2.0 < x .AND x < 3.0) PRINT *, x

In this case if x is between 2 and 3 then it will be printed.

    Another IF form used when multiple statements are required for the TRUE case is:

    IF (logical -criteria) THEN
        execution statements
    END IF

Notice that all execution statements depending on the IF statement are indented.  This is not required, but it makes your code more readable.  All the execution statements are performed if the logical statement is TRUE.  Otherwise, all information between the IF and END IF statements are ignored.  An example:

    IF (x  >= 0) THEN
        z = x * y
        PRINT *, "x is a positive number."
    END IF
 

IF-ELSE

    The ELSE statement allows specification of execution statements for the case that the logical-criteria of the IF statement is FALSE.

    IF (logical -criteria) THEN
        execution statements for true result
    ELSE
        execution statements for false result
    END IF

If the logical-criteria is TRUE then the first set of statements are performed and the second are bypassed.   If the logical-criteria is FALSE then the first set of statements are bypassed and the second statements performed.  As an example:

    IF (x > 0) THEN
        PRINT *, "The value is greater than zero."
    ELSE
        PRINT *, "The value is not greater than zero."
    END IF
 

IF-ELSE IF

   Nested IF statements  (IF statements within IF statements )can allow for many selection criteria.  For example:

    IF (x >= 1) THEN
        PRINT *, "The value is greater than or equal to one."
    ELSE
        IF (x > 0) THEN
            PRINT *, "The value is between zero and one."
        ELSE
            PRINT *, "The value is less than or equal to zero."
        END IF
    END IF

Although nested loops are allowed, they can frequently become very complex if many selection options are desired.  The ELSE IF statement allows for multialternative selection in a more simple and easier to read format.   In general:

    IF (logical -criteria1) THEN
        execution statements 1
    ELSE IF (logical -criteria2) THEN
        execution statements 2
    ELSE IF (logical -criteria3) THEN
        execution statements 3
    .....
    ELSE
        execution statements n
    END IF

The ELSE statement is optional, but it indicates the statements to be performed if none of the IF statements are TRUE.  Only one set of execution statements are performed.  The expressions are evaluated in order and when a TRUE statement is found, its execution statements will be performed and execution continues with the next statement following the END IF statement.  For example:

    IF (x > 0) THEN
        PRINT *, "Value is greater than zero."
    ELSE IF (x < 0) THEN
       PRINT *, "Value is less than zero."
    ELSE IF (x == 1) THEN
        PRINT *, "Value is one."
    ELSE
        PRINT *, "Value is zero."
    END IF

Notice if x is equal to one the output of this series of commands will be:

    Value is greater than zero.

It will not be "Value is one." because the first true IF statement is that it is less than zero.  That statement was executed and execution proceeded outside of the IF-ELSE IF construct.
 

SELECT CASE

    The SELECT CASE construct is not as general as the IF constructs, but it is of use for some specific applications.  In general:

    SELECT CASE (selector)
        CASE (list1)
            execution statements 1
        CASE (list2)
            execution statements 2
        ...
         CASE (listn)
           execution statements n
    END SELECT

The selector is either an integer, character, or logical expression.  It cannot be realList1 to listn are the possible values for the selector.  Say you wanted to associate output a letter grade for a known a numerical score.  Say the numerical score is associated with the real variable named grade:

    SELECT CASE (INT(grade))    !  The real value grade is converted to an integer.
        CASE (90:)                          !  90: indicates values of 90 or above.
            PRINT *, "Your grade is an A."
        CASE (80:89)                       ! 80:89 means 80 to 89.
            PRINT *, "Your grade is a B."
        CASE (70:79)
            PRINT *, "Your grade is a C."
        CASE (60:69)
            PRINT *, "Your grade is a D."
        CASE (:59)                          !  59: indicates 59 or below.
            PRINT *, "Your grade is an F."
    END SELECT
 

NAMING

    All of these branching options can be named in order to improve the readability of your code.  For the previous example of a nested IF:

    positive: IF (x >= 1) THEN
        PRINT *, "The value is greater than or equal to one."
    ELSE
        negative:  IF (x > 0) THEN
            PRINT *, "The value is between zero and one."
        ELSE
            PRINT *, "The value is less than or equal to zero."
        END IF negative
    END IF positive
 

    SELECT CASE constructs can be named in much the same way.

       name:  SELECT CASE (selector)
        CASE (list1)
            execution statements 1
        CASE (list2)
            execution statements 2
        ...
         CASE (listn)
            execution statements n
    END SELECT name
 
 
« previous next»