new do-while syntax

Blair P. Houghton bph at buengc.BU.EDU
Mon Dec 19 09:15:02 AEST 1988


In article <864 at calvin.EE.CORNELL.EDU> johns at calvin.ee.cornell.edu.UUCP (John Sahr) writes:
>>In article <8536 at alice.UUCP>, ark at alice.UUCP (Andrew Koenig) writes:
>[suggesting a do-while syntax with the loop test in the middle]
>
>I think the point of Mr. Koenig's proposal was to handle larger loops than
>the literal interpretation of his example indicated.  Consider
>>> 	do {
>>> 		ch = getchar();
>		ch &= MASK;
>		ch  = table_look_up[ch];
>		report_status(ch);
>>> 	} while (ch != EOF) {
>>> 		process(ch);
>		process2(ch);
>		process3(ch);
>>> 	}
>
>This too, is contrived.

Pardon my two-cent kibbitz, but what's wrong with using the comma
operator to do that for which it is ideally suited?

I find the syntax as described above to be confusing.  All of the
"ch =" statements are in the do-while loop, and all of the "process()"
statements are subsequent to the loop, no?  No.  But it seems so.
(Unless of course I've got it exactly backwards; It Can Happen, especially
when I'm entering a discussion I just now discovered.)

The K&R-conformant version would be:

	do {
		process(ch);
		process2(ch);
		process3(ch);
	} while (
		ch = getchar(),
		ch &= MASK,
		ch  = table_look_up[ch],
		report_status(ch),
		ch != EOF
	);

See?  No muss, no fuss, sez who only curly-brackets get special
indentation, and you get to tell your grandchildren that you once
used the comma operator, which I consider one of the prime elegances
of the C language.

				--Blair
				  ",,,,"



More information about the Comp.lang.c mailing list