[comp.lang.c] loops in general

raw@math.arizona.edu (04/10/90)

In article <1274@proxima.UUCP> you write:
>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

First, I never said that ansi C should contain a loop repeat as above.  This
 was added by some one else.

>
>Funny, I've never used a LOOP .. EXIT construct, not even in those PASCAL

The language I was progamming had only the above loop stmt test stmt rpeat
structure to replace while{} and do {} while.  I found it much easier and
more intuitve to use, but it took practice to use effectively, just like any
other construct in any language you chose to mention.  Most other languages
offer something similar.  In C for(;;){ stmt {break on test} stmt } or
 while (true) { stmt {break on test} stmt} for instance.  But IMHO these are
misuse, yes even perversions of the intent of the language.

> 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

There was no multiple exit from the loop construct I mentioned.  Only misuses
of for or while stmts permit multiple exits.  I also dislike this.   Looking
for the loop exit was easy.  Included was a 'formater' that indented the code.
I used it as a debugger.

> thirdly,
>as I said, it's unnecessary, at least to me;

While loops are also unnecessary as FORTRAN proves over and over again.  But
they are convenient and make code easier to understand.  My point is that the
loop construct I like is, IMHO, more convenient that whiles and do whiles.

> 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.

Nor am I.  My sleep is continuous also.

>
>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

IMHO,  bleech!  But a reasonable way to do it in C.
personally, I prefer

#define forever while(1)



			Richard Walter

ccplumb@lion.waterloo.edu (Colin Plumb) (04/10/90)

Actually, C already has a loop syntax that could be extended to
exit-in-the-middle loops.  It would be fun to hack this into GCC.  I
presume the standard would require its usual one diagnostic about the
use of extensions and then be silent on the issue.

Anyway, C currently has

while (expr) stmt
and
do stmt while (expr);

However, ';' is a legal statement, so how about treating these as
special cases of

[do stmt] while (expr) stmt

So I could write a loop like

do 
	c = getchar();
while (c != EOF || !feof(stdin)) {
	<rest of loop>
}

Perl's continue blocks are also nifty, but I don't know of a really
elegant way to add them, and goto cont; ... ; continue: ...; works
just as well.
-- 
	-Colin

scott@bbxsda.UUCP (Scott Amspoker) (04/13/90)

In article <1565@amethyst.math.arizona.edu> raw@math.arizona.edu () writes:
>>
>>#define		forever		for(;;)
>
>personally, I prefer
>
>#define forever while(1)

I've always wondered why some people use 'while(1)'  as an unconditional
loop instead of 'for(;;)'.  A 'while' loop, by definition, has a condition
(granted, the condition is always true).  A 'for' loop has an optional
condition and therefore, may be truly "unconditional".

Oh well, just $.02 from an anal retentive programmer :-)


-- 
Scott Amspoker
Basis International, Albuquerque, NM
(505) 345-5232
unmvax.cs.unm.edu!bbx!bbxsda!scott

roland@cochise.pcs.com (Roland Rambau) (05/08/90)

CCDN@levels.sait.edu.au (david newall) writes:

->Why not call a spade a spade?  If you want an unconditional loop, then
->use a goto.  It suffers no apparent ambiguity; it needs no optimisation;
->and it exactly expresses what you want.

If I want an unconditional loop, I use a loop-statement!

( and I #define loop as for(;;) --
I too have seen while(1) to result in larger code )

The trouble with goto is that You need to introduce a name for the label,
while with for(;;) the brackets are sufficient. And from Murphy's law
follows: If you have a label, somebody will falsly jump to it.
Roland Rambau

  rra@cochise.pcs.com,   {unido|pyramid}!pcsbst!rra,   2:507/414.2.fidonet 

roland@cochise.pcs.com (Roland Rambau) (05/08/90)

al@uunet!unhd (Anthony Lapadula) writes:

->Here's an example of my typical usage, presented for dissection by the net.

Let me add more stuff for burning:

I have used 'goto's even in cases where I could easily have avoided them,
to improve the clearity of the code:

- Example 1:  a rather longish sequence of IO-operations to a graphic
              board, where the right _ordering_ is quite critical and not
              at all obvious -- and in one place I may detect that I've got
              an vacuous result and have to restart somewhat earlier in
              this sequence ( you may call it 'backtrack' )

  I decided to use 'goto' because a loop construct would have obscured
  the sequential nature of this function. ( This code is being distributed
  as a template to our customers )

- Example 2:  an ( a bit to complicated ) main loop of a little interactive
              tool, which I choose to modell after the psychological
              S-R-loop paradigma ( Stimulus-Response ). Now when starting
              this loop I had to execute a certain start-stimulus first,
              giving:

                 for( S = start ;; S = Stimulus() ) {
                   Response(S) ;
                 }

              but I found this confusing, and now use instead

                 S = start ;
                 goto Start_here ;

                 loop {

                   S = Stimulus() ;

                 Start_here:

                   Response(S) ;
                 }

It just matches my internal modell better.


Roland Rambau

  rra@cochise.pcs.com,   {unido|pyramid}!pcsbst!rra,   2:507/414.2.fidonet