Fortran/Fortran control

From Wikibooks, open books for an open world
Jump to navigation Jump to search

← Fortran Simple math | Fortran procedures and functions →

Part of the Fortran WikiBook



if (...) then ... else ... end if

Fortran has a block-if statement of the form:

 if (logical expression1)      then
    ''Lines of Fortran''
 else if (logical expression2) then
    ''Lines of Fortran''
 else if (logical expression3) then
    ''Lines of Fortran''
    ''Lines of Fortran''
 end if

The following operators can be used when making the logical expression:

  • Greater than or less than
 .GT. .LT. > <
  • Greater than or equal to or less than or equal to
 .GE. .LE. >= <=
  • Equal to and Not equal to (Note: The Fortran standard mandates that these cannot be used with logicals but some compilers will not enforce the standard)
 .EQ. .NE. == /=
  • Equal to, Not equal to, and Not for logicals

To check more than one statement, use .AND. or .OR.:

 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
    else if (x < x2) then
    end if
 end program xif

There are two interesting archaic forms of IF:

 IF (''logicalExpression'') GO TO ''lineNumber''
 IF (''arithmeticExpression'') ''firstLineNumber'', ''secondLineNumber'', ''thirdLineNumber''

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
    print *,"You can put more stuff here."
 else if (month == "March")    then
    num_days = 31
    num_days = 30
 end if

can be replaced by

 select case (month)
    case ("January", "December")
       num_days = 31
    case ("February")
       num_days = 28
       print *,"You can put more stuff here."
    case ("March")
       num_days = 31
    case default
       num_days = 30
 end select

Fortran does not need a break statement.


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
 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
 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
    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 line 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 instead in these cases.

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. start, end, and increment can be any variable name.

 do i=start,end,increment
     ''code goes here''
 end do

Simple statements[edit]

GO TO statementNumber will jump to the specified statement number.

STOP conditionCode 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.