The DO construct specifies the repeated execution (loop) of a block of code.
A DO statement begins a DO construct.
An END DO statement ends the innermost nested DO construct.
Syntax[construct-name :] DO [label] [loop-control]
construct-name is an optional name given to the DO construct
label is the optional label of a statement that terminates the DO construct
loop-control is [,] do-variable=expr, expr [, expr]or [,] WHILE (while-expr)
do-variable is a scalar variable of type INTEGER
expr is a scalar expression of type INTEGERthe first expr is the initial value of do-variable; the second expr is the final value of do-variable; the third expr is the increment value for do-variable.
while-expr is a scalar LOGICAL expression
block is a sequence of zero or more statements or executable constructs
do-termination is END DO [construct-name]or label action-stmt
action-stmt is a statement other thanGOTO, RETURN, STOP, EXIT, CYCLE, assigned GOTO, arithmetic IF, or END
If a do-variable is present, the expressions in are evaluated, and do-variable is assigned an initial value and an iteration count. An iteration count of zero is possible. Note that because the iteration count is established before execution of the loop, changing the do-variable within the range of the loop has no effect on the number of iterations.
If loop-control is WHILE (while-expr), while-expr is evaluated and if false, the loop terminates.
If there is no loop-control it is as if the iteration count were effectively infinite.
Use of default or double-precision REAL for the do-variable has been removed from the Fortran 95 language.
The CYCLE Statement skips to the next iteration of a DO loop.
The EXIT Statement exits a DO loop altogether.
If the DO statement specifies a label, the corresponding do-termination statement must be identified with the same label.
If a construct name is specified in the DO statement, the same construct name must be specified in a corresponding END DO statement.
If the DO statement is not identified by a construct name, the do termination statement must not specify a construct name.
Ending a DO construct with a labeled action statement is obsolescent, the use of END DO is preferred.
Example 1integer :: i real :: a=20.,b=10. do i=1,10 ! code block goes here end do do ! infinite do loop ! better have some way to leave exit end do do while (a > b) ! does while condition is true a=a-1. write(*,*) a > b end do do i=10,1,-1 ! backward iteration of index i ! code block goes here end do
Example 2integer :: i, j outer_loop: do i=1,5 inner_loop: do j=1,5 if(i>j) then write(*,*) ' cycling inner' cycle inner_loop else if (i<j) then write(*,*) ' cycling outer' cycle outer_loop else write(*,*) i,j end if end do inner_loop end do outer_loop
Example 3integer :: i do 10, i=1,10 10 end do ! label number required do i=1,10 end do lp: do while(i>10) i=i-1 end do lp ! construct name required
Example 4integer :: i real :: a=20., b=10. do i=1,5 write(*,*) 'simple indexed do' end do do i=1,5,2 write(*,*) 'indexed do with stride' end do do i=5,1 write(*,*) 'zero trip loop' end do do while (a > b) ! does while condition is true a=a-1. write(*,*) a > b end do