I get "error #6602: A CYCLE or EXIT statement must not be used with a non-DO block." with the snippet below. My reading of the Fortran standard is that it is valid, am I correct? If the block is named and the exit has the name there is no error.
block !error #6602: A CYCLE or EXIT statement must not be used with a non-DO block. exit l1 = 1 end block test: block ! no error exit test l1 = 1 end block test
The F2008 standard says in 8.1.10:
C844 (R850) If a construct-name appears, the EXIT statement shall be within that construct; otherwise, it
5 shall be within the range (126.96.36.199) of at least one do-construct.
For the first BLOCK of your program, the words following "otherwise" apply, since it has no name, and the compiler would see that it is lacking: at least one do-construct should have the EXIT in its range. Agree?
2 1 The EXIT statement provides one way of terminating a loop, or completing execution of another construct.
3 R850 exit-stmt is EXIT [ construct-name ]
4 C844 (R850) If a construct-name appears, the EXIT statement shall be within that construct; otherwise, it 5 shall be within the range (188.8.131.52) of at least one do-construct.
Sure that makes sense that if exit refers to a construct name it must be within that construct. The otherwise does not seem so clear to me but I guess you would need more text in the std to cope with nested blocks if you took my initial interpretation. So be it! I will let it rest in piece.
The reason for this is that EXIT was introduced in F2003 as a way of terminating loops (BLOCK did not exist at the time.) If an EXIT without a name could be used to exit a BLOCK, it could change the meaning of a program where a DO now contained a BLOCK. I agree it seems puzzling at first, but in general the standards committee is reluctant to change the meaning of an existing feature in conforming code.
In support of what was chosen by the standards committee, consider a DO loop without a BLOCK ... END BLOCK but also containing an EXIT that is to exit the loop. Now then subsequent to this, you revise the code such that the EXIT is now contained within the (unnamed) BLOCK. At this point, it is ambiguous as to where the EXIT is to go to/escape from.