[comp.lang.c] Bodyless loop

karl@haddock.ima.isc.com (Karl Heuer) (10/19/90)

Style wars beginning.  Hit 'n' now.

In article <1990Oct15.042851.18595@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
>In article <3343@idunno.Princeton.EDU> pfalstad@drops.Princeton.EDU (Paul John Falstad) writes:
>>	while(...);
>
>By the way, tacking the ";" on the end of the while is a great way to
>trip up your readers...

This is a valid concern.  On the other hand, there are those of us who gag at
the sight of a naked semicolon on a line by itself, which is what I guess
Henry to be suggesting as an alternative.

Some time ago I adopted the convention of writing bodyless loops as
	do; while (...);
so that it's impossible to accidentally leave out the final semicolon.  This
produced an immediate bonus in that it often revealed a further transformation
that simplified the code:
	while ((rpid = wait(&status)) != pid);
==>	do; while ((rpid = wait(&status)) != pid);
==>	do rpid = wait(&status); while (rpid != pid);

The use of an assignment as a subexpression of a condition is only necessary
for test-at-top loops; thinking of it as a test-at-bottom loop eliminates the
need.  (This assumes that, all else being equal, "a=b; if (P(a))" is better
than "if (P(a=b))".  That leads back to the ":= vs = vs ==" wars, which should
probably go to comp.lang.misc.)

Exercise: evaluate each of the forms below (insert newlines according to your
personal style) and rank them according to susceptibility to disastrous typo
and/or reader misunderstanding.  Send your results by USmail to the person
whose name is immediately under yours in the Phone book.
	while (...);
	while (...) /* nothing */;
	while (...) {}
	while (...) continue;
	do; while (...);
	do /* nothing */; while (...);

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint

henry@zoo.toronto.edu (Henry Spencer) (10/19/90)

In article <18573@haddock.ima.isc.com> karl@ima.isc.com (Karl Heuer) writes:
>>By the way, tacking the ";" on the end of the while is a great way to
>>trip up your readers...
>
>This is a valid concern.  On the other hand, there are those of us who gag at
>the sight of a naked semicolon on a line by itself, which is what I guess
>Henry to be suggesting as an alternative.

Actually, no, my preferred alternative is

	while (...)
		continue;

although I can live with minor variants like using {} instead of continue.
(This is, incidentally, just about the only use of continue that I'm happy
with!)
-- 
The type syntax for C is essentially   | Henry Spencer at U of Toronto Zoology
unparsable.             --Rob Pike     |  henry@zoo.toronto.edu   utzoo!henry

jh4o+@andrew.cmu.edu (Jeffrey T. Hutzelman) (10/20/90)

how about these:

#define ever (;;)
#define nothing ;

Then you get

for ever
	{
	...
	}

and

do nothing while (...);
-----------------
Jeffrey Hutzelman
America Online: JeffreyH11
Internet/BITNET:jh4o+@andrew.cmu.edu, jhutz@drycas.club.cc.cmu.edu,
                jh4o@cmuccvma

>> Apple // Forever!!! <<

floyd@hayes.ims.alaska.edu (Floyd Davidson) (10/20/90)

In article <kb7nFyq00Vp5QS7UVi@andrew.cmu.edu> jh4o+@andrew.cmu.edu (Jeffrey T. Hutzelman) writes:
>how about these:
>
>#define ever (;;)
>#define nothing ;
>
>Then you get
>
>for ever
>	{
>	...
>	}
>
>and
>
>do nothing while (...);
>-----------------
>Jeffrey Hutzelman
>America Online: JeffreyH11
>Internet/BITNET:jh4o+@andrew.cmu.edu, jhutz@drycas.club.cc.cmu.edu,
>                jh4o@cmuccvma
>
>>> Apple // Forever!!! <<


I looked back about 50-100 articles trying to see if this 
was supposed to be funny, or if you meant it.  I couldn't
find anything that gave me a clue.

Do you actually put that in program code?


-- 
Floyd L. Davidson      floyd@hayes.ims.alaska.edu
8347 Richardson Hwy.   floydd@chinet.chi.il.us
Salcha, AK 99714       [and related to Alascom, Inc. by a pay check, only]

daveg@near.cs.caltech.edu (Dave Gillespie) (10/22/90)

>>>>> On 19 Oct 90 15:50:17 GMT, henry@zoo.toronto.edu (Henry Spencer) said:

> In article <18573@haddock.ima.isc.com> karl@ima.isc.com (Karl Heuer) writes:
>>By the way, tacking the ";" on the end of the while is a great way to
>>trip up your readers...
>
> Actually, no, my preferred alternative is

> 	while (...)
> 		continue;

My favorite way is to write

	while (...) ;

wish a space before the trailing semicolon.  It's concise, and
unambigious (to me at least) because I never put a space before
the semicolon for any other reason.  (Nor do most C programmers
I've seen.)

								-- Dave
--
Dave Gillespie
  256-80 Caltech Pasadena CA USA 91125
  daveg@csvax.cs.caltech.edu, ...!cit-vax!daveg