[comp.lang.misc] Structured loops

pardo@june.cs.washington.edu (David Keppel) (01/25/89)

>>[Ongoing discussion of Turing loop structure]

In article <12@euteal.UUCP> mart@euteal.UUCP (Mart van Stiphout) writes:
>[ C's "do", "for", "while" vs. "loop" structring ]
>[What about the following 1.5 loop?]
> while ( i = read() )   { ... code .... }

The above C fragment lets you put only restricted kinds of code inside
the `while' condition (a 1.25 loop?).

There is a more general construct that I've not seen in any existing
programming languages, although (a) doubtless it exists and (b) the
idea is easily a decade old (ok, probably 30, but I *know* at least
10) Substitute your favorite C/Pascal/Lisp/Smalltalk syntax.  In
meta-Algol:

	do ... while boolean ... od;


The conventional "while" loop looks like:

	do while (condition)
	    ...
	od;

The "repeat until" looks like:

	do
	    ...
	while (condition) od;

A single-exit 1.5 loop (most of my 1.5 loops are single exit) looks
like:

	do
	    ...
	while (condition)
	    ...
	od;

The multi-exit 1.5 loop is still less structured.

	;-D on  ( Do ... while ... crazy ... )  Pardo
-- 
		    pardo@cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo

oster@dewey.soe.berkeley.edu (David Phillip Oster) (01/25/89)

When my for loop expressions get too complex in C, I prefer:

	for(;;){
		...arbitrary statement...
		if(exitExpression1){
			break;
		}
		...
	}

Seems like Turing just gives this idiom more prominence.

dave@emerald.PRC.Unisys.COM (David Lee Matuszek) (01/26/89)

In article <7053@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes:

>There is a more general construct that I've not seen in any existing
>programming languages, although (a) doubtless it exists and (b) the
>idea is easily a decade old (ok, probably 30, but I *know* at least
>10) Substitute your favorite C/Pascal/Lisp/Smalltalk syntax.  In
>meta-Algol:
>
>	do ... while boolean ... od;

This is the same as the loop ... exit when <boolean> ... end loop;
except that the sense of the test is reversed.  This exists in Turing.
It also exists in an in-house language I designed and implemented a couple
of years ago.  I don't know of any other languages that use it, however.

I checked a little further and found that Knuth (Structured
Programming with go to Statements, ACM Computing Surveys, v6, n4,
December 1974, p. 261-300; see page 279) credits Ole-Johan Dahl with
"recently" inventing:  "loop; S; while ~B; T; repeat;", but he does
not cite a reference.  (He strongly implies that the negation on the B
is important, but when I read the paragraph carefully I found it very
ambiguous as to just what he thought was better.)
-- Dave Matuszek (dave@prc.unisys.com)
-- Unisys Corp. / Paoli Research Center / PO Box 517 / Paoli PA  19301
-- Standard disclaimer:  Any resemblance between my opinions and those of my
   employer is strictly coincidental.

smryan@garth.UUCP (s m ryan) (01/26/89)

Dijkstra's loop is

	do guard -> statement
	|  guard -> statement
	| ...
	od

As long as at least one guard is true, execute a statement with a true guard.
If more than one guard is true, only one a statement is executed and it can
be any with a true guard. If no guard is true, the loop exits.

This loop does not permit an unconditional statement which is executed within
the loop before any guard, so Someone extended the construct

	do unconditional_statement
	|  guard -> statement
	|    . . .
	od

This loop cannot have an exit value and exit conditions are not explictly
specified, so Someoneelse extended the construct once more

	do unconditional_statement
	|  guard -> statement
	|   . . .
	|  guard -> expression exit
	|   . . .
	od

This loop executes the top statement and then selects a true guard. If no
guard is true Something Bad happens. If a guarded statement, the statement
is executed and the loop begins again. If a guarded expression exit, the
value of the loop is the value of the expression and the loop is exitted.

My contribution would be

	struct unconditional_statement
	|      guard -> statement again
	|      .  .  .
	|      guard -> expression exit
	|      .  .  .
	tcurts

which includes all the other structures:

	begin				struct skip
	  s				|      true -> s exit
	end				tcurts

	if p				struct skip
	 then s				|      p -> s exit
	 else t				|      not p -> t exit
	fi				tcurts

	if p -> s			struct skip
	|  q -> t			|      p -> s exit
	fi				|      q -> t exit
					tcurts

	while p				struct skip
	 do s od			|      p -> s again
					|      not p -> skip exit
					tcurts

	repeat s			struct s
	 until p			|      p -> skip again
					|      not p -> skip exit
					tcurts

My thought is to provide a single clause to the parser and then use macros
to make it pretty, rather than put all the complexity in a difficult to
change parser.
-- 
When it was caught, then Loki said,                                 -- s m ryan
`What fish is this from river's bed?
Your doom is near; to Hel you'll fly                           -- Andwari's Gem
unless with gold your life you buy.'                                    -- 1/10

peter@ficc.uu.net (Peter da Silva) (01/27/89)

In article <9045@burdvax.PRC.Unisys.COM>, dave@emerald.PRC.Unisys.COM (David Lee Matuszek) writes:
> This is the same as the loop ... exit when <boolean> ... end loop;
> except that the sense of the test is reversed.  This exists in Turing.
> It also exists in an in-house language I designed and implemented a couple
> of years ago.  I don't know of any other languages that use it, however.

The Bourne shell uses it.

	while list; do list; done

And Forth.

	begin ... while ... repeat

And an assembly preprocessor Karl and I designed.

	.begin
	...
	.while condition-codes
	...
	.repeat

And of course this is equivalent to the common 'C' idiom.

	while(1)
	{
		...
		if(whatever) break;
		...
	}
-- 
Peter da Silva, Xenix Support, Ferranti International Controls Corporation.
Work: uunet.uu.net!ficc!peter, peter@ficc.uu.net, +1 713 274 5180.   `-_-'
Home: bigtex!texbell!sugar!peter, peter@sugar.uu.net.                 'U`
Opinions may not represent the policies of FICC or the Xenix Support group.

lth@uoregon.uoregon.edu (Lars Thomas Hansen) (01/27/89)

In article <7053@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes:
>>>[Ongoing discussion of Turing loop structure]
>
>There is a more general construct that I've not seen in any existing
>programming languages, although (a) doubtless it exists and (b) the
>idea is easily a decade old (ok, probably 30, but I *know* at least
>10) Substitute your favorite C/Pascal/Lisp/Smalltalk syntax.  In
>meta-Algol:
>
>	do ... while boolean ... od;
>
>
[useful examples deleted ]

The do ... while <condition>  ... enddo construct exists in a not-too-well-
known language in this part of the world, namely in the application language
of "NorSoft", a Norwegian application generator/database system, which has
a remarkably powerful language (with which I am hopelessly in love and wish I
could use for other things as well :-)

As an aside, I think that Pascal's While-Do and C's while/do-while/for(;;)
constructs are infinitely more readable than the general (admittedly power-
ful) loop construct in Turing.

hi ho
--lth

jack@cs.glasgow.ac.uk (Jack Campin) (01/27/89)

pardo@cs.washington.edu (David Keppel) wrote:
  >>[Ongoing discussion of Turing loop structure]
  
>  There is a more general construct that I've not seen in any existing
>  programming languages, although (a) doubtless it exists and (b) the
>  idea is easily a decade old (ok, probably 30, but I *know* at least
>  10) Substitute your favorite C/Pascal/Lisp/Smalltalk syntax.  In
>  meta-Algol:
  
>  	do ... while boolean ... od;

This construct exists in S- (and PS-) algol, and I find it one of the less
lovable features of those languages.  The actual syntax is

	repeat	<void statement>
	while	<boolean valued statement>
	do	<void statement>

where either the repeat-part or the do-part may be omitted.

The problem is that if you're reading a piece of alien code and you see a
"while <boolean>", you have no idea whether it's the beginning, middle, or end
of a control construct without scanning a LOT of surrounding context.  The
construct itself is fine; economizing on keywords like this in the concrete
syntax isn't.

-- 
Jack Campin  *  Computing Science Department, Glasgow University, 17 Lilybank
Gardens, Glasgow G12 8QQ, SCOTLAND.    041 339 8855 x6045 wk  041 556 1878 ho
INTERNET: jack%cs.glasgow.ac.uk@nss.cs.ucl.ac.uk    USENET: jack@glasgow.uucp
JANET: jack@uk.ac.glasgow.cs     PLINGnet: ...mcvax!ukc!cs.glasgow.ac.uk!jack

piet@ruuinf (Piet van Oostrum) (01/27/89)

In article <9045@burdvax.PRC.Unisys.COM>, dave@emerald (David Lee Matuszek) writes:
 `
 `This is the same as the loop ... exit when <boolean> ... end loop;
 `except that the sense of the test is reversed.  This exists in Turing.
 `It also exists in an in-house language I designed and implemented a couple
 `of years ago.  I don't know of any other languages that use it, however.
 `
Modula-2, Modula-3 and Ada have LOOP ... EXIT ... END (LOOP) constructions
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806. piet@cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)

chl@r1.uucp (Charles Lindsey) (02/04/89)

In article <2894@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>In article <9045@burdvax.PRC.Unisys.COM>, dave@emerald.PRC.Unisys.COM (David Lee Matuszek) writes:
>> This is the same as the loop ... exit when <boolean> ... end loop;
>> except that the sense of the test is reversed.  This exists in Turing.
>> It also exists in an in-house language I designed and implemented a couple
>> of years ago.  I don't know of any other languages that use it, however.
>
>[Examples of languages that use it]

>[and lots more examples from other people]

ALGOL 68
	WHILE	read data;
		NOT end of data
	DO	process data
	OD

SETL
	LOOP
	DOING	read(a, b)
	WHILE	NOT EOF
	DO	someset WITH [a, b]
	END LOOP

C.H.Lindsey	chl@ux.cs.man.ac.uk