Previous Table of Contents Next


1.2.13.4. The do Construct

The looping construct in Fortran is the do construct. The general form of the do construct is

   do loop control
        block of statements
   end do

The block of statements, called the loop body or do construct body, is executed repeatedly as indicated by the loop control.

There are three types of loop control. In one instance, the loop control is missing, in which case the loop is executed until some explicit instruction in the do body such as an exit statement terminates the loop. In the second type of loop control, a variable takes on a progression of values until some limit is reached. The third type is executed while a logical expression is true.

Construct Names

A do construct may have a construct name on its first statement. It consists of an ordinary Fortran name followed by a colon. The end do statement that ends the construct must be followed by the same construct name. This permits more complete checking that do constructs are nested properly and provides a means of exiting or cycling more than one level of nested loop.

The exit Statement

The exit statement causes termination of execution of a loop. If the keyword exit is followed by the name of a do construct, that named loop (and all loops nested within it) is exited.

The cycle Statement

The cycle statement causes termination of the execution of one iteration of a loop. If the keyword cycle is followed by the name of a construct, all loops nested within that named loop are exited and control is transferred back to the beginning of the block of statements that comprise the named do construct.

Loops with No Loop Control

For a do construct with no loop control, the block of statements between the do statement and the matching end do statement is executed repeatedly until an exit statement causes it to terminate.

Suppose you want to print out all powers of 2 that are less than 1000. This is done with a simple do construct with no loop control and an exit statement:

   program some_powers_of_2

      integer :: power_of_2

      power_of_2 = 1 ! The zero power of 2
      print_power: do
         print *, power_of_2
         power_of_2 = 2 * power_of_2
         if (power_of_2 >= 1000) then
            exit print_power
         end if
      end do print_power
   end program some_powers_of_2

Loop Control with a do Variable

This type of loop control provides a simple means of assigning successive values to a variable each time an iteration of a loop is executed. A simple example that prints the squares and cubes of the integers 1 to 20 follows:

   do number = 1, 20
      print *, number, number ** 2, number ** 3
   end do

The do while Construct

The do while form of the do construct allows a loop to be executed as long as a logical condition is true. This is illustrated by a simple example of a loop that is executed until some iterative process converges:

   converged = .false.
   do while (.not. converged)
      call iter_8 (data, converged)
   end do

1.2.14. Modules and Procedures

Modules provide a place to put data declarations so that they can be used and shared by programs. Modules also provide the place to put a procedure, which is either a function or a subroutine, and to put definitions of user-defined types; these are basic building blocks of a program and are usually used by more than one part of a program.

Modules are especially useful when building a package or library of data and procedures that may be accessible to many different programs.

1.2.14.1. Modules

A module is a program unit that is not executed directly but contains data specifications and procedures that may be utilized by other program units via the use statement.

Writing and Using Modules

To begin with a simple example, one use of a module is to include the definition of constants that might be useful in programs. The module math_module contains the values of pi, e, and g; of course, it could contain many more useful constants:

   module math_module

      real, public, parameter :: pi = &
            3.1415926535897932384626433832795028841972
      real, public, parameter :: e = &
            2.7182818284590452353602874713526624977572
      real, public, parameter :: g = &
            0.5772156649015328606065120900824024310422

   end module math_module

Any program that needs these constants can simply use the module:

   program circle

      use math_module
      real :: radius, area

      radius = 2.2
      area = pi * radius ** 2
      print *, area

   end program circle

It is also possible to declare variables in a module. The module declarations_module declares logical variables flag_1 and flag_2, which could then be used in any program that uses the module:

   module declarations_module

      logical, public :: flag_1, flag_2
   end module declarations_module

Any program or procedures that use a module share the values of variables declared in the module. Thus, changing the value of such a variable in one procedure causes it to change in all procedures that use the module:

   program using_modules

      use declarations_module
      logical, parameter :: f = .false.
      flag_1 = f
      flag_2 = .not. f
         . . .
   end program using_modules

The form of a typical module is

   module module name
      use statements
      private
      access statements
      type definitions
      type declarations
   contains
      subroutines and functions
   end module module name

The access statement consists of either private or public followed by a colon and a list of the names of procedures in the module. The access determines whether the procedure is available outside the module. Other entities, such as types, variables, and parameters, may have public or private as an attribute.


Previous Table of Contents Next