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

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
Fortran offers two ways to end a 'Do' loop: 'End Do' and 'Continue', with the latter being the older syntax. The 'Do ... Continue' method can be advantageous for performance, as compilers can optimize it by inserting a CONTINUE statement for loop jumps, potentially improving execution speed. However, the 'Do ... End Do' syntax, introduced in Fortran 77, is generally considered more intuitive and easier to read, as it does not require line labels. While both methods are valid, modern programming practices favor the newer 'End Do' for clarity. Ultimately, the choice may depend on specific coding standards or personal preference.
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).
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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
3K
  • · Replies 20 ·
Replies
20
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K