Fortran

Guide To Learn

Using a do loop to iterate

We need to implement looping in our app to do two things:

  1. Loop over array elements to set initial values of water height and calculate the solution at the next time step.
  2. Loop for a number of time steps to iterate the numerical solution forward in time.

The main construct for looping or iterating in Fortran is the do loop:

do n = start, end     ❶
  ...                 ❷
end do

❶ Increment n from start to end.

❷ Code inside the loop will execute end – start + 1 times.

Here, n is the integer index that changes value with each iteration. In the first iteration, n equals start. When the program reaches the end do line, n is incremented by one. Internally, the program then checks if n is larger than end. If yes, the program breaks out of the loop and proceeds to the code that follows the loop. Otherwise, the control is returned to the beginning of the loop and another iteration is done. The process repeats until the program exits the loop.

By default, do loops increment the counter by one. However, you can specify a custom integer increment immediately after the end index:

do n = start, end, increment    ❶
  ...                           ❷
end do

❶ Increment n from start to end by increment.

❷ Code inside the do loop will execute (end – start) / increment + 1 times.

In this case, the loop begins with n equal to start and is incremented by the value of increment with each iteration.

There are several rules to remember when coding do loops:

  • The loop index (n) must be an integer variable (not a parameter).
  • startend, and increment must be integers of either sign. They can be variables, parameters, or expressions that evaluate to integer values.
  • If start equals end, the loop body will execute only once.
  • If start is greater than end and increment is positive, the loop body will not execute.
  • If start is less than end and increment is negative, the loop body will not execute.
  • A bare do statement without the counter and start and end indices is an infinite do loop that has to be broken out of by other means, such as the exit statement.
  • Loops can be nested (loops inside loops).
  • Loops can be named. This is useful for nested loops where you want to associate an end do with its matching do, as shown in the following listing.

Listing 2.10 Using names with nested do loops

outer_loop: do j = 1, jm          ❶
  inner_loop: do i = 1, im        ❷
    print *, 'i, j = ', i, j      ❸
  end do inner_loop               ❹
end do outer_loop                 ❺

❶ Begins a named outer loop (slower varying)

❷ Begins a named inner loop (faster varying)

❸ This could be any code that we want to iterate.

❹ Closing the inner loop

❺ Closing the outer loop

Although naming loops may at first seem unnecessarily verbose, the names become useful in larger programs with multiple levels of nesting. Furthermore, you can use loop names to break out of a specific do loop from any level using the exit statement.

Finally, the general syntax form of a do loop is shown in figure 2.5.

Figure 2.5 General syntax of a Fortran do loop. Optional syntax elements are in square brackets.

In the figure, expr1expr2, and expr3 are start and end indices (inclusive), and the increment, respectively. name can be any given name.

Using a do loop to iterate

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top