TExcelTAD
Home › Fortran › Fortran Do Loops

Fortran Do Loops

The do loop repeats a block of statements. Its most common form counts an integer variable from a start value to an end value.

fortran · try it yourself
program countdown
  implicit none
  integer :: i
  do i = 1, 5
    print '(a, i0)', 'Iteration ', i
  end do
end program countdown
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

The general form is do var = start, end, step. The step is optional (default 1) and may be negative:

fortran · try it yourself
program steps
  implicit none
  integer :: i

  do i = 0, 20, 5
    write(*, '(i4)', advance='no') i
  end do
  print *

  do i = 10, 1, -3
    write(*, '(i4)', advance='no') i
  end do
  print *

  do i = 5, 1           ! start > end with a positive step: zero iterations
    print *, 'never printed'
  end do
  print '(a)', 'done'
end program steps
   0   5  10  15  20
  10   7   4   1
done

A print * with nothing after it ends the current line.

How Fortran counts

Before the loop starts, Fortran computes the trip count from start, end and step. Changing the end value inside the loop does not change the number of iterations, and you are not allowed to assign to the loop variable inside the loop. After the loop finishes normally, the loop variable holds the value it would have had in the next iteration:

fortran · try it yourself
program after_loop
  implicit none
  integer :: i, n
  n = 3
  do i = 1, n
    n = 10           ! does not affect the trip count
  end do
  print '(a, i0)', 'i after the loop: ', i
  print '(a, i0)', 'n after the loop: ', n
end program after_loop
i after the loop: 4
n after the loop: 10

Accumulating results

Loops are often used to build sums, products and other running totals. Initialise the accumulator before the loop:

fortran · try it yourself
program accumulate
  use iso_fortran_env, only: real64, int64
  implicit none
  integer :: k
  integer(int64) :: factorial
  real(real64) :: series

  factorial = 1
  do k = 1, 20
    factorial = factorial * k
  end do
  print '(a, i0)', '20! = ', factorial

  ! Leibniz series: pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...
  series = 0.0_real64
  do k = 0, 99999
    series = series + (-1.0_real64)**k / (2*k + 1)
  end do
  print '(a, f12.8)', 'pi approx = ', 4 * series
end program accumulate
20! = 2432902008176640000
pi approx =   3.14158265

20! needs a 64-bit integer: it is far larger than the ~2.1 billion limit of a default integer.

Nested loops

Loops can be nested to work through two dimensions, such as the rows and columns of a table:

fortran · try it yourself
program times_table
  implicit none
  integer :: row, col
  do row = 1, 5
    do col = 1, 5
      write(*, '(i4)', advance='no') row * col
    end do
    print *
  end do
end program times_table
   1   2   3   4   5
   2   4   6   8  10
   3   6   9  12  15
   4   8  12  16  20
   5  10  15  20  25

Real-valued steps: avoid them

Loop variables must be integers in modern Fortran (real loop variables were deleted from the standard). To step through real values, loop over an integer and compute the real value from it. This also avoids accumulating rounding errors:

fortran · try it yourself
program real_steps
  implicit none
  integer :: i
  real :: x
  do i = 0, 4
    x = 0.25 * i
    print '(f5.2, f9.4)', x, x**2
  end do
end program real_steps
 0.00   0.0000
 0.25   0.0625
 0.50   0.2500
 0.75   0.5625
 1.00   1.0000

Tip: The implied do loop (expr, i = start, end) builds a list in one line, e.g. print '(5i4)', (i**2, i = 1, 5). It appears again in the arrays lessons.

Test yourself

How many times does do i = 10, 1 execute its body?

  1. 10
  2. 9
  3. 0
  4. 1

What is the value of i after do i = 1, 3 ... end do completes normally?

  1. 3
  2. 4
  3. 0
  4. Undefined

Which loop prints 9, 6, 3?

  1. do i = 9, 3
  2. do i = 9, 3, -3
  3. do i = 3, 9, 3
  4. do i = 9, 1, 3