Suggested new C loop syntax (was `if (a = b)')

Fred Fish fnf at estinc.UUCP
Fri Dec 23 03:24:16 AEST 1988


In article <264 at twwells.uucp> bill at twwells.UUCP (T. William Wells) writes:
<: < In article <8536 at alice.UUCP<, ark at alice.UUCP (Andrew Koenig) writes:
<: < <   do {
<: < <           ch = getchar();
<: < <   } while (ch != EOF) {
<: < <           process(ch);
<: < <   }
<
<	do
<		statement
<	while (expr);
<		statement
<
<See the difference? Well, in case you didn't, it is the semicolon at
<the end of the while expression. This is damned hard to spot and does
<not lead to a syntax error.

Since this is my pet peeve #1 about C style, I couldn't resist.  There
is no ambiguity if you simply make it a habit to ALWAYS include braces
for {if, while, do, for} etc, even when they are not strictly necessary.

I was recently helping out a freshman CS major with her C homework and
found out, to my horror, that the teacher in the course was marking
off points for code which included braces when they weren't strictly
required.  They were teaching her to write code like:

	inchar = getchar ();
	while (inchar != EOF)
		if (inchar == 0)
			goto done;
		else
			inchar = getchar ();
done:	return inchar;

Ickkk!!!
-Fred
-- 
# Fred Fish, 1346 West 10th Place, Tempe, AZ 85281,  USA
# asuvax!nud!fishpond!estinc!fnf          (602) 921-1113



More information about the Comp.lang.c mailing list