Article View: comp.lang.modula2
Article #3033Re: Arrays of Records and Sequential File Reading
From: Peter Moylan
Date: Mon, 13 Nov 1995 23:22
Date: Mon, 13 Nov 1995 23:22
43 lines
2821 bytes
2821 bytes
jthorpe@island.net wrote:
>The purists will kill me for this, but the LOOP END; EXITing when a hit is
>made is a very useful tool. The academics will say it's crude and you can do
>the same thing with either REPEAT UNTIL or DO WHILE but there are lots of
>times when one hasn't the time or the inclination to struggle through one or
>the other.
I'm pretty much of a purist, but in this case I agree with you.
In fact I code the initial version of _all_ my loops with the
LOOP construct, and there are usually multiple exits because
of things like error checking and special case checking. Once
I've convinced myself that I've covered all cases, I look to see
whether tests can be combined. If the end result is an exit
at the beginning or end of the block, I then change the loop
into a WHILE or REPEAT loop, as appropriate.
In my experience, programmers often have trouble with loops, for
two reasons:
1. A real-life loop is rarely of the kind "WHILE simple-condition
DO something". In practice the condition is a complicated one
because there are multiple conditions to test. For example,
programmers often fail to see that "WHILE p^ <> x" should have
been written as something like "WHILE (p <> NIL) AND (p^ <> x)".
(In Pascal it's even worse, because the standard doesn't
specify short-circuit evaluation of conditions.)
2. If you stick to the WHILE and REPEAT forms, then you have to
make a decision at the outset - i.e. before coding the loop
body - about whether the exit test should be at the beginning
or the end. In my opinion it's better to make that decision
after you've worked out what should be in the body.
As a result, I believe that LOOP actually gives cleaner coding
practices - although I do believe in converting a LOOP into
something simpler, if appropriate, once I've finished the first
draft of the code.
Multiple exits don't bother me, because the exits all jump to
the same point, i.e. the code is still well-structured.
--
Peter Moylan peter@ee.newcastle.edu.au
ftp://ee.newcastle.edu.au/pub/www/Moylan.html
Technical Details
Message-ID:
<488jva$1np@seagoon.newcastle.edu.au>Path:
arc1-ca.pugleaf.net!fourier.newcastle.edu.au!peterReferences:
<46ngu2$r72@grid.Direct.CA> <DH2EpC.MIo@cix.compulink.co.uk> <472lcc$kdk@grid.Direct.CA> <474odt$npe@p235.informatik.uni-Bremen.de> <47a4vn$s5g@grid.Direct.CA> <47h0k9$esj@cliff.island.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fourier.newcastle.edu.au!peter From: peter@fourier.newcastle.edu.au (Peter Moylan) Newsgroups: comp.lang.modula2 Subject: Re: Arrays of Records and Sequential File Reading Date: 13 Nov 1995 23:22:18 GMT Organization: The University of Newcastle Message-ID: <488jva$1np@seagoon.newcastle.edu.au> References: <46ngu2$r72@grid.Direct.CA> <DH2EpC.MIo@cix.compulink.co.uk> <472lcc$kdk@grid.Direct.CA> <474odt$npe@p235.informatik.uni-Bremen.de> <47a4vn$s5g@grid.Direct.CA> <47h0k9$esj@cliff.island.net> Reply-To: peter@tesla.newcastle.edu.au NNTP-Posting-Host: fourier.newcastle.edu.au X-Newsreader: TIN [version 1.2 PL2]