for(;;) vs. while(1) is a draw

diamond@tkovoa diamond at tkou02.enet.dec.com
Wed May 30 11:52:18 AEST 1990


Followups to comp.lang.c, where most of this belongs.

In article <3093 at goanna.cs.rmit.oz.au> ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:

>I loathe and detest while (1) because it's obfuscatory.
>The thing is, a skilled programmer uses a while (..) statement because
>that's the way it fell out of his preconditions, postconditions,
>invariant, and so on.  You use 'while' to express "here after this
>keyword is all the information you need about when the loop stops."

Exactly the same reasoning applies to the second expression in a
"for" statement.  You use "for" to express "the second expression
after this keyword is all the information you need about when the
loop stops."
(My opinion and argument here are serious, not intended to be satirical.)

>The character sequence (;;) has, I believe, no other use in C.  It's a
>very clear signal "no, it _wasn't_ a mistake, I really _did_ mean to
>put the real loop control somewhere else."

while(1) is an equally clear signal.

>In fact, I would welcome a "lint" option that reported 'break' or
>'continue' statements having a while (..) or do while (..) statement
>as scope as being likely errors.

I loathe and detest all "break" and "continue" statements.  The
language should have been designed differently.  Nonetheless,
"break" and "continue" are suitable for while(1) and for(;;) loops.
Reading becomes difficult when "break" or "continue" occurs in either
a "while" or "for" loop with non-constant terminating condition.

>Um, I've recently had a lot more contact with people from a Pascal
>background than I really wanted.  Quickly now, what's wrong with this:
>	program main;
>	    var i: integer;
>	    procedure p(n: integer);
>		begin
>		    for i := 1 to n do write(' ');
>		    writeln(i);
>		end;
>	    begin
>		p(10);
>	    end.
>(I count three violations of the Pascal Standard.)

Then read the Pascal Standard and count again.  There is one violation.
(Did you think that Pascal lacks a null statement?)

-- 
Norman Diamond, Nihon DEC     diamond at tkou02.enet.dec.com
Proposed group comp.networks.load-reduction:  send your "yes" vote to /dev/null.



More information about the Comp.lang.c mailing list