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
AI Thread 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).
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
8
Views
4K
Replies
3
Views
2K
Replies
8
Views
2K
Replies
12
Views
3K
Replies
8
Views
4K
Replies
9
Views
2K
Replies
20
Views
6K
Replies
4
Views
2K
Back
Top