I strongly advise never to use an un-tagged EXIT statement. We write tools to detect software errors. The number of errors associated with un-tagged EXIT and LEAVE statements is alarming. Tag the loop and tag the EXIT and the danger is removed.
" EXIT [ construct-name ]
If a construct-name appears on an EXIT statement, the EXIT statement
shall be within that construct; otherwise, it shall be within at least
one do-construct. "
Is there a specific option (flag) in the Fortran compilers that warns about possible errors in this case?
…
I think it would be interesting to mention this in fortran-lang Best Practices
John, What do you imply by “tagging” ?
Are you requiring setting an error_code associated with the EXIT ?
When I have an optional EXIT, or to test if a “DO scan” was unsuccessfull, I will often use the “If (do_index <= do_limit ) then” (for +ve step) test to identify that the Do loop did not run to completion.
I am not convinced that this approach is alarming ?
do i=1,n
jloop: do j=1,n
if (x(i,j) == 0) exit jloop
end do jloop
end do
and not
do i=1,n
do j=1,n
if (x(i,j) == 0) exit
end do
end do
even though the two codes are equivalent. It should be explicit what loop the programmer intends to exit. I think one reason is that if you put an additional loop inside the loop over j, the effect of exit will change, even if that was not intended.
@Beliavsky is exactly right. An EXIT statement with no construct identifier is a GO SOMEPLACE statement. The errors occur when additional dimensions are added to arrays which require existing code to be enclosed in additional loops.
LEAVE was a precursor to EXIT. It was implemented in Gould-SEL extended FORTRAN 77. The Gould-SEL systems were the preferred machines for high performance simulation and instrument control from about 1978 to 1990. They were used in flight simulators, power-plant simulators and for control of instruments like tracking radars. The codes live on in emulation, even now. The optional argument to LEAVE was (is?) the end label of a DO loop.
I have considered the design of “EXIT label” and “CYCLE label” poor for another reason.
The options for the value of label are extremely limited.
In the example above there are no options available for label.
I think the syntax could have been made more flexible so that the following could be allowed to include the DO do-variable.
do i=1,n
do j=1,n
if (x(i,j) == 0) exit i
end do
end do
Givel the very limited label options, it should not be too difficult for a compiler to identify what “i” means.