[comp.lang.c] loops in c

raw@math.arizona.edu (03/29/90)

In article <4543@daffy.cs.wisc.edu> you write:
>In article <KZB#G{_@rpi.edu> night@pawl.rpi.edu (Trip Martin) writes:
>| An interesting note that might give some perspective to this issue is how
>| Plus, a little-known language that I'm somewhat familiar with, handles loops.
>| Instead of having both while and do..while forms of loops, it uses one form
>| called cycle.  Cycle is an infinite loop.  You can then stick exit statements
>| anywhere in the loop (and then can be conditional, with both flavors of tests
>| supported).  Conceptually, it's a more general way of handling loops.
>
>This is the second request I've seen for a loop construct that allows an
>exit from anywhere in the loop.  Is there something drastically wrong with:
>
>for(;;) {
>   <statements>
>   if (expr)
>      break;
>   <statements>
>}
>
>or have I missed something here?
>
>Rick (schaut@garfield.cs.wisc.edu)

The point you missed Rick, is that this is a _perversion_ of a for loop.  The
same nastiness could be written with a while or a do...while.  My point is
the cycle statement above and the the loop ... test ... repeat I talked about
are the **NATUAL** looping structures in their respective languages.  These 
constructs allow a more natural structure to a program IMNSHO and are _easier_
to use.

If I saw C code like the above for ... if then break, I would be tempted to
shoot the person and/or persons responsible.  IMO the loop should be rewritten.
The reason that code like the above is written is because to many people it is
a natural way to do it.  You have to be _trained_ *NOT* to author code like
that.

So lets make it easier on all.  Demand a loop {stmnt} test {stmnt} repeat
in ANSI C!!!!!!!!!!!!!!!!!!!!!!!!!!!


			Richard Walter

-------------------------------------------------------------------------------

			Keep on crunching those numbers

-------------------------------------------------------------------------------

lucio@proxima.UUCP (Lucio de Re) (04/07/90)

In article <1546@amethyst.math.arizona.edu> raw@math.arizona.edu writes:
>
>So lets make it easier on all.  Demand a loop {stmnt} test {stmnt} repeat
>in ANSI C!!!!!!!!!!!!!!!!!!!!!!!!!!!
> 
>                        Richard Walter

Funny, I've never used a LOOP .. EXIT construct, not even in those PASCAL
days when it was (sometimes) available. Firstly, I don't like looking
for the exit from a loop construct; secondly it allows multiple exits,
which run counter to my structured programming orientation and thirdly,
as I said, it's unnecessary, at least to me; I have written a lot of
code and I am not embarrassed to set a condition variable to tell me
whether the loop should be repeated or terminated, nor is the ineffi-
ciency (space only, for that matter) of a preliminary loop execution
likely to cause me to lose any sleep.

A friend who thinks efficiency comes foremost was puzzled by the following
COBOL excerpt:

	PERFORM READREC.
	PERFORM READREC UNTIL ZEOF.

where

READREC.
	READ REC AT END set ZEOF conditional.
	
(An abbreviation; what need I say, I gave up COBOL whenever I could.)

I felt it was rather elegant (if it can be called that!) and the space
inefficiency was minute. For those who don't know COBOL, the UNTIL
clause is tested at the BEGINNING of the loop, while a plain PERFORM
executes once only.

Also, I _have_ used for(;;) which my personal "util.h" defines:

#define		forever		for(;;)

where I don't expect the loop to terminate except when the power
switch is used. No breaks in those loops. Fussy, yes; I also don't
use do {} while () except when pushed (and against my better judgement)
because in FORTRAN the default execution of loops at least once gave
me lots of headaches I have no intention of repeating and seldom even
exit() from a program except at the end, at the cost of additional
nesting (I'd love a 132 column screen).

So that's my buzz. Anybody out there feels the way I do? Anybody out
there has read "A Discipline of Programming" by Dijkstra and would
like to compare (C) notes?

----------------------------------------------------------------------
I used to design nuclear reactors and you should see the code that
engineers and scientists come up with. Yuuuck! Spaghetti everywhere.
-------------------------------------------------- (Kenneth L Moore) -
Lucio de Re                        ...uunet!ddsw1!olsa99!proxima!lucio
-------------------------------------------------------- lucio@proxima
                Disclaimer: I hope he was joking!