For basic looping, Fortran provides a flexible construct called the do-loop:

do loop_variable = loop_start, loop_end, loop_increment
  ...
enddo
Fortran

Specifying the loop_increment is optional, and it may be omitted if equal to 1. If loop_increment is present, it may be positive or negative (for counting down). Here are examples with different increments:

do i=1, 10      ! this loop is executed 10 times
x = x + 1       ! i takes values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
enddo

do j=3, 8, 2    ! this loop is executed 3 times
  x = x + 1     ! j takes values 3, 5, 7
enddo

do j=8, -1, -2  ! this loop is executed 5 times
  x = x + 1     ! j takes on values 8, 6, 4, 2, 0
enddo
Fortran

To go along with its simple do-loop, Fortran provides a do-while construct, allowing for more complex exit conditions.

do while (y <= 15)
  x = x + 1
  y = x**2
enddo
Fortran
 
© 2025  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Access Statement
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)