[net.lang.pascal] Multiple loop exits

ark@ut-sally.UUCP (Arthur M. Keller) (08/08/86)

First, let me say that I am opposed to unrestricted use of GOTOs.
However, I do like the ability to prematurely exit from a construct.
The argument that premature exits from constructs (FOR loops,
functions, etc.) represent multiple exits from the construct is
misguided.  There is only one exit from the construct.  There are
merely multiple ways of getting there.  Having an explicit exit
in a construct is often better than implicitly falling through to
the bottom.

Consider the following way of writing the linear search using invented syntax:

found := false;
FOR "search foo" index := low TO high DO
	IF foo[index]=key
	    THEN location := index;
		EXIT "search foo";
		ENDIF;
ENDFOR "search foo";

Semantics of naming BEGINs and ENDs:
A loop may be named at either the BEGIN or the END.  Both will name the
block.  If both are present, they must match exactly.

Semantics of the EXIT construct:
EXIT causes execution to continue at the end of the named BEGIN-END block
if a name is specified, or at the end of the innermost BEGIN-END block
if no name is specified.

To implement the semantics of CONTINUE using the EXIT construct:

FOR "outer" ....
    BEGIN "inner"
	EXIT "inner"; (* means CONTINUE with the next iteration of the loop *)
	EXIT "outer"; (* means leave the FOR loop altogether *)
    END "inner";
ENDFOR "outer";

An EXIT is particularly helpful for thinking like:

you know you're done when
	abcdfefgtgf
  or   ajsadjdfjaldjla
  or   asdasdsadsadasd

Once you make any of these true, you can exit.

An EXIT is also helpful for handling exceptions and errors without
unnecessarily cluttering up code to ensure than nothing is done
in the event of an error.

Arthur
-- 
------------------------------------------------------------------------------
Arpanet: ARK@SALLY.UTEXAS.EDU
UUCP:    {gatech,harvard,ihnp4,pyramid,seismo}!ut-sally!ark