Why are there Two Ways to End a 'Do' Loop in Fortran: 'End Do' vs 'Continue'?

  • Context: Fortran 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Fortran
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 18K views
Saladsamurai
Messages
3,009
Reaction score
7
Can someone tell me why there exist 2 different ways of indicating the end of a 'Do' loop? Is one advantageous over the other? Or is one just an older way of doing it?

What do you tend to use?
 
Physics news on Phys.org
Saladsamurai said:
Can someone tell me why there exist 2 different ways of indicating the end of a 'Do' loop? Is one advantageous over the other? Or is one just an older way of doing it?

What do you tend to use?

There are two ways because Fortran has been around for a long time. The DO ... CONTINUE variant is older than the DO ... END DO form, which I believe came about in Fortran 77. If I were writing Fortran these days, I would go with the newer syntax.

BTW, you should post these programming questions in the right section -- Programming & Comp. Sci.
 
DO/CONTINUE is actually advantageous over DO/ENDDO due to how FORTRAN compilers work. Compilers have a preallocated amount of memory to go over a check-list of sorts, one of which is inserting continue statements in DO loops. The compiler will insert a CONTINUE statement in place of the ENDDO to create a jumpback to the top of the loop. The computation time between the two methods is small, but still there. You can test this out by making a simple program of the form

Code:
program test
implicit none

[declare vars]
real tarray(2), result

call etime(tarray, result)

do 10 i=1,1000000000000 (to take up some time)
   some computation involving i
10 continue

call etime(tarray, result)
print*, result

return
end program

If you do a few test runs and get an average and compare to an averaged DO/ENDDO loop in place of DO/CONTINUE, you will see the increase in speed.
 
In addition to Mark44's reply, "ye' Old Days" also allow us to end our loops with a line labels (ie, line number) attached to an executable statement - yuch.

Better practice suggested (although didn't require) using a continue statement with, of course, an attached line label.

The use of the "END DO" does not require a line labels and is more inuitive to program structure while reading the code. (all those CONTINUEs tended, at times, to get muddled).