The DO statement is the first statement in a DO loop. This statement will complete control of the number of repetitions for any iterative procedure that appears between it and the end of the loop.
In FORTRAN there are several valid ways to control the execution of a DO loop. The earliest and still legitimate form of the DO loop involves a loop end marked by a statement label. Frequently this labeled statement at the end of the loop is a continue statement. The continue statement basically tells the program to continue looping using the next value in the index. It does not tell the computer to go on and execute the next executable statement in the source listing. For instance, a simple DO loop from an early piece of code might appear as follows.
do 100 i=2,200,2 xarray(i-1)=i 100 continue
It could also be written as below with the same results.
do 100 i=2,200,2 100 xarray(i-1)=i
Before any other DO variations are introduced, there are few things that should be pointed out first. The i=2,200,2 in the example tells the computer to start the loop with i equal to two, end when i is equal to two hundred and increment i by two every time through the loop. Therefore, the first time through the loop i will be equal to two, the second time four, the third time six, so on and so forth until i is finally equal to two hundred. Another thing is that even though the last pass through this loop will use a value of i equal to 200, the value of i coming out of the loop will equal two hundred and two. You should be aware of this if it is ever necessary to use the value of the index (here "i") after completion of the loop.
Now consider a version of the DO loop that was introduced in Fortran 90 which eliminated the need to use a statement label and the continue statement. Instead what is used to terminate the DO loop is the END DO statement. This means that a DO loop written in Fortran 90 might appear like this.
do i=1,100 asum = xarray(i) + yarray(i) + asum end do
Besides ending the DO loop with the END DO statement instead of the labeled CONTINUE statement, there is one other difference between this loop and the previous one. If you look at the first line, no value is given to the computer to tell how to increment i. When this is the case, the computer automatically assumes that i will be incremented by one. This little bit of short hand has existed in all versions of FORTRAN, since most loops are incremented by one.
Another variation of the DO loop that was introduced with Fortran 90 is the named DO loop. This can be handy if you write a program that has several DO loops embedded inside of each other. Each loop begins with a name and is terminated with an END DO statement that carries the same name as the loop which it terminates. This way there can be no confusion as to which loop is being terminated with an END DO statement. It should be noted that these named DO loops can not be terminated with a simple unnamed END DO or by the labeled CONTINUE statement. They must be terminated with the named END DO. With this in mind, the previous DO loop could take on the following appearance if it was named.
loop1: do i=1,100 asum= xarray(i) + yarray(i) + asum end DO loop1
There is one final thing that you should be aware of that concerns DO loops. In all of the example loops illustrated above, INTEGER values were used for the bounds and increment indexes. In the current FORTRAN standard it is permissible to use both INTEGER and REAL numbers in a DO loop. That means that the following DO statement is perfectly permissible.
However, recall that floating point arithmetic does not always guarantee that 3.1 added three times to one will precisely equal 10.3. The possibility exists that it may be slightly less than or slightly greater than 10.3. This fact can screw up the DO loop operation. In fact, it is highly likely that the ability to use REAL numbers in the bounds and index of a DO loop may be removed from a future FORTRAN standard for this reason. Therefore, you should avoid using REAL numbers in this capacity.
Implied DO loops are DO loops in the sense that they control the execution of some iterative procedure, but are different than DO loops because they do not use the do statement to control the execution. Basically, these loops are a shorthand that was introduced in FORTRAN to provide a method for array initialization and to cut back on the number of lines of code that where required in the program. For instance, look at the next three blocks of code. All three of the blocks initialize an array in a different manner. The first block will load initial values into an array using a standard DO loop.
integer a(10) do 100 i=1,10 a(i)=i 100 continue
Now to initialize the same array with a data statement.
integer a(10) data a/1,2,3,4,5,6,7,8,9,10/
Finally here is how an array can be initialized with an implied DO loop. Take notice of how much shorter and easier in is to initialize this array with the implied do.
integer :: a(10)=(/(j,j=1,10)/)
The similarity to an actual DO loop should be pretty obvious as the index and bounds to a DO loop appear inside of the parentheses. The compiler interprets this in exactly the same way that it does the DO loop in the first block of code above. Beside working inside of the integer and real type declaration statement an implied do will also work with a write statement and with a data statement. The next two lines of code illustrate how these loops can be used with these statements.
write (*,2000) (a(j),j=1,10) data (a(j),j=1,10)/10*8/
For more information on implied DO loops check out lecture nineteen.
examples: speed.f and trapeziod.f
Written by Jason Wehr : firstname.lastname@example.org and Maintained by John Mahaffy : email@example.com