🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.pascal
Article #323

Multiple loop exits (was linear search revisited)

#323
From: Arthur M. Keller
Date: Fri, 08 Aug 1986 09:40
56 lines
2490 bytes
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
Technical Details
Message-ID: <5520@ut-sally.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!im4u!ut-sally!ark
References: <871@minster.UUCP> <2785@umcp-cs.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!im4u!ut-sally!ark
From: ark@ut-sally.UUCP (Arthur M. Keller)
Newsgroups: net.lang.pascal
Subject: Multiple loop exits (was linear search revisited)
Message-ID: <5520@ut-sally.UUCP>
Date: Fri, 8-Aug-86 05:40:16 EDT
Article-I.D.: ut-sally.5520
Posted: Fri Aug  8 05:40:16 1986
Date-Received: Sat, 9-Aug-86 06:38:51 EDT
References: <871@minster.UUCP> <2785@umcp-cs.UUCP>
Reply-To: ark@sally.UUCP (Arthur M. Keller)
Organization: U. Texas CS Dept., Austin, Texas
Lines: 56