FORTRAN DO Statement

Home Computational Physics FORTRAN FORTRAN DO Statement


The DO Statement

This statement makes it possible to execute a section of a program repeatedly, with automatic changes in the value of an integer variable between repetitions.

Examples
This code will print 10 numbers on the screen.
DO 10 I=1,10
WRITE (6,*) I
10        CONTINUE
Note : It is not permitted within the range of a DO to change the value of I

Same code, but uses FORMAT command.
DO 10 I=1,10
WRITE (6,20) I
10        CONTINUE
20        FORMAT( I2 )
Note : Do not insert any DIMENSION and FORMAT statements and the various type-statements within the DO loops.

This will print 10 numbers in descending order.
DO 10 I=10,1,-1
WRITE (6,*) I
10        CONTINUE


This one prints 10 numbers with the step size of 2.
DO 10 I=1,10,2
WRITE (6,*) I
10        CONTINUE


Here is a nested DO loops.
DO 20 I=1,10
DO 10 J=1,5
WRITE (6,*) I*J
10                    CONTINUE
20        CONTINUE


Instead of using two CONTINUE statements you can use one.
DO 20 I=1,10
DO 20 J=1,5
WRITE (6,*) I*J
20        CONTINUE


When using several DO loops, do not mixed up the order. As to this diagram do not intersect the lines.
Note : When using several DO loops, do not mixed up the order. As to the above diagram do not intersect the lines.