Fortran/Program flow control

(Redirected from Fortran/Fortran control)


Selection

edit

If-then(-else) conditional

edit

Conditional execution is done using the if, then and else statements in the following construct:

 if (logical_expression1) then
    ! Block of code
 else if (logical_expression2) then
    ! Block of code
 else
    ! Block of code
 end if

You may have as many else if statements as you desire.

The following operators can be used when making expressions:

Operation Modern Fortran Old FORTRAN
Less than < .LT.
Greater than > .GT.
Greater than/equal >= .GE.
Less than/equal <= .LE.
Equal == .EQ.
Not equal /= .NE.
Logical equivalent .EQV.
Logical not equivalent .NEQV.
Logical not .NOT.
Logical and .AND.
Logical or .OR.

Note: The Fortran standard mandates .EQ. and .NEQ. cannot be used with logicals but some compilers will not enforce the standard

To check more than one statement, use parentheses.

if ((a .gt. b) .and. .not. (a .lt. c)) then

The following program generates a random number between 0 and 1 and tests if it is between 0 and 0.3, 0.3 and 0.6, or between 0.6 and 1.0.

program xif
    implicit none
    real :: x
    real, parameter :: x1 = 0.3, x2 = 0.6

    call random_seed()
    call random_number(x)
    if (x < x1) then
        print *, x, "<",x1
    else if (x < x2) then
        print *, x, "<", x2
    else
        print *, x, ">=", x2
    end if
end program xif

There are two interesting archaic forms of IF:

      IF (<logical_expression>) GOTO <statement_label>
      IF (<arithmetic_expression>) <first_label>, <second_label>, <third_label>

In the first form, things are pretty straightforward. In the second form, the arithmetic expression is evaluated. If the expression evaluates to a negative number, then execution continues at the first line number. If the expression evaluates to zero, then execution continues at the second line number. Otherwise, execution continues at the third line number.

case (switch)

edit
select case(...) case (...); ... end select

If an if block consists of repeated tests on a single variable, it may be possible to replace it with a select case construct. For example, the code

if (month=="January" .or. month=="December") then
    num_days = 31
else if (month=="February") then
    num_days = 28
else if (month=="March") then
    num_days = 31
else
    num_days = 30
end if

can be replaced by

select case (month)
    case ("January", "December")
        num_days = 31
    case ("February")
        num_days = 28
    case ("March")
        num_days = 31
    case default
        num_days = 30
end select

Fortran does not need a break statement.

Loops

edit
do i=1,10 ... end do

To iterate, Fortran has a do loop. The following loop prints the squares of the integers from 1 to 10:

do i=1,10
    print *, i**2
end do

One can exit a loop early using exit, as shown in the code below, which prints the squares of integers until one of the squares exceeds 25.

do i=1,10
    isquare = i**2
    if (isquare > 25) exit
    print *, isquare
end do

Loops can be nested. The following code prints powers 2 through 4 of the integers from 1 to 10

do i=1,10
    do ipower=1,3
        print *, i, ipower, i**ipower
    end do
end do

In an archaic form of DO, a line number on which the loop(s) end is used. Here's the same loop, explicitly stating that label 1 is the last line of each loop:

      DO 1 i=1,10
          DO 1 ipower=1,3
              1 PRINT *, i, ipower, i**ipower

If using the archaic form, the loop must not end on an IF or GO TO statement. You may use a CONTINUE statement as an anchor for a the 1 label.

There is also an optional increment argument when declaring a do loop. The following will count up by two's. 2, 4, 6, ...

do i=2,10,2
    write (*,*) i
end do

Arguments to the do loop don't have to be numbers, they can be any integer that is defined elsewhere in the program. first, last, and increment can be any variable name.

do i=first,last,increment
    ! Code goes here
end do

Simple statements

edit

goto statement_label will jump to the specified statement number.

stop exit_code will stop with the specified condition code or exit code. stop may be coded without an argument. Note that on many systems, stop 0 is still a failure. Also note that pre-Fortran 2008, the condition code must be a constant expression and not a variable.

exit will leave a loop.

continue can be used to end an archaic DO loop when it would otherwise end on an IF.

cycle will transfer the control of the program to the next end do statement.

return leaves a subroutine or function.