Jump to content

Fortran/Program flow control: Difference between revisions

From Wikibooks, open books for an open world
[unreviewed revision][unreviewed revision]
m Programming:Fortran control moved to Fortran/Fortran control: to comply with wikibooks standard naming policy
(No difference)

Revision as of 11:27, 3 July 2006

Part of the Fortran WikiBook

Selection

if

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
else
   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
.EQ. ==

To check more than one statement, use .AND. and .OR.

IF ((a .GT. b) .AND. (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


case (switch)

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

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

if      (month == "January")  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
else
   num_days = 30
end if

can be replaced by

select case (month)
   case ("January")
      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.

Iteration

do (for)

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


There is also an optional incriment arguemnet when declairing a do loop. The following will count up by two's. 2, 4, 6, ...

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

Arguements 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