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

  • Fortran
  • Thread starter Saladsamurai
  • Start date
  • Tags
    Fortran
In summary, the "END DO" variant is newer and more advantageous due to the use of continue statements in FORTRAN compilers.
  • #1
Saladsamurai
3,020
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
  • #2
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.
 
  • #3
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.
 
  • #4
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).
 
  • #5


The reason for having two different ways of indicating the end of a 'Do' loop in Fortran is due to historical reasons. 'End Do' was introduced in Fortran 90, while 'Continue' was used in earlier versions of Fortran.

Both 'End Do' and 'Continue' serve the same purpose of terminating a 'Do' loop. However, 'End Do' is considered a more modern and structured way of ending a loop, as it clearly indicates the end of the loop and improves the readability of the code. On the other hand, 'Continue' can sometimes lead to confusion as it is also used in other contexts, such as in 'Do While' loops.

As a scientist, it is important to use the most efficient and clear way of writing code. In this case, 'End Do' would be the preferred method as it is the more modern and structured approach. However, if you are working with legacy code or collaborating with others who are more familiar with 'Continue', it may be necessary to use that method for consistency and compatibility. Ultimately, the choice between 'End Do' and 'Continue' should be based on the specific context and the preference of the programmer.
 

1. What is the difference between "End Do" and "Continue" in Fortran?

"End Do" is used to mark the end of a do-loop in Fortran, while "Continue" is used to skip the current iteration of the loop and continue with the next one.

2. Can "End Do" be used in nested loops?

Yes, "End Do" can be used in nested loops to mark the end of the innermost loop.

3. When should "Continue" be used in a Fortran program?

"Continue" should be used when you want to skip the current iteration of a loop based on certain conditions, but continue with the rest of the loop. This can be useful in situations where you want to exclude certain values from the loop.

4. Is there a limit to the number of "Continue" statements that can be used in a single loop?

No, there is no limit to the number of "Continue" statements that can be used in a single loop. However, using too many "Continue" statements can make the code more complex and difficult to understand.

5. Can "End Do" and "Continue" be used interchangeably in Fortran?

No, "End Do" and "Continue" serve different purposes in Fortran and cannot be used interchangeably. "End Do" marks the end of a loop, while "Continue" skips the current iteration of the loop.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
Replies
3
Views
319
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
Replies
15
Views
1K
Back
Top