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
Click For Summary

Discussion Overview

The discussion revolves around the existence of two different ways to end a 'Do' loop in Fortran: 'End Do' and 'Continue'. Participants explore the historical context, advantages, and personal preferences regarding these syntax options.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants note that the DO ... CONTINUE form is older than the DO ... END DO form, which was introduced in Fortran 77.
  • One participant suggests that the newer syntax (DO ... END DO) is preferable for modern programming practices.
  • Another participant argues that DO ... CONTINUE may have performance advantages due to how Fortran compilers handle memory and jump statements, although the difference in computation time is described as small.
  • A participant mentions that using line labels with CONTINUE statements was common in older practices, but the END DO syntax is seen as more intuitive and clearer for program structure.

Areas of Agreement / Disagreement

Participants express differing opinions on the advantages of each syntax, with no clear consensus on which is definitively better. The discussion remains unresolved regarding the overall preference and performance implications of the two methods.

Contextual Notes

Some claims about performance differences depend on specific compiler implementations and may vary based on the context of use. The discussion does not resolve these technical nuances.

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?
 
Technology 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).
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 20 ·
Replies
20
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K