Article View: comp.lang.misc
Article #5384Re: Loops
From: Dave Mason
Date: Mon, 28 Mar 1988 04:52
Date: Mon, 28 Mar 1988 04:52
72 lines
2402 bytes
2402 bytes
In article <2773@mmintl.UUCP> franka@mmintl.UUCP (Frank Adams) writes:
>
>The more I program, the more convinced I become that the 'while' loop is a
>mistake. It just doesn't match the semantics of too many real loops.
>...
>The key points are:
>
>(1) Exit from any point in the loop.
>(2) Multiple exits.
>(3) Code can be associated with any exit point.
>
In a language I designed for my students to implement in my compiler
course loops look like:
do
...
while expr
...
od
where the while can go anywhere & there can be any number of them (or
until's which are similar but with opposite logic). This leads to
Pascal style while & repeat loops:
do while expr
...
od
do
...
until expr od
as well as the more flexible forms. The do keyword can be followed by
1 or more 'for' clauses, which are stepped in parallel:
int a[0..29]
...
do
for i in 1..20
for ap in a
...
while i!=ap
...
od
equivalent to the C:
int a[30];
...
{int i,*ap;
for (i=1,ap=a;i<=20 && ap<&a[sizeof(a)/sizeof(int)];++i,++ap) {
...
if (i!=*ap) break;
...
}}
Notes:
1) loop control variables have a scope of the loop
2) loop control variables traversing an array (or list or other linked
structure) are aliases for the current element
3) all the efficiency of C pointer arithmetic
4) all the safety of Pascal subscript calculation (and more if we were
to make the aliased array be not directly usable in the loop
scope, but I want them to be able to implement a reasonable
subset in a semester :-)
5) controlled break out of outer loops (continue, break) (do, while,
until, continue, break all have optional labels)
From the sample programs I've written in this language, this seems
like a very reasonable and natural loop structure.
(BTW, Frank's idea already exists in at least Concurrent Euclid, Turing:
loop
...
exit when a<>b
...
end loop
)
../Dave
Technical Details
Message-ID:
<294@tmsoft.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!tmsoft!masonReferences:
<2827@enea.se> <1557@pasteur.Berkeley.Edu> <2773@mmintl.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!tmsoft!mason From: mason@tmsoft.UUCP (Dave Mason) Newsgroups: comp.lang.misc Subject: Re: Loops Message-ID: <294@tmsoft.UUCP> Date: 28 Mar 88 04:52:36 GMT References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> <2773@mmintl.UUCP> Reply-To: mason@tmsoft.UUCP (Dave Mason) Followup-To: comp.lang.misc Organization: TM Software Associates, Toronto / Ryerson Polytechnical Institute Lines: 72