OT: for(;;) vs while(1)

Ken Irving ki.lists at alaska.edu
Mon Nov 1 00:42:55 UTC 2010


On Sun, Oct 31, 2010 at 05:17:34PM -0500, Rob Landley wrote:
> On Thursday 28 October 2010 16:10:35 Denys Vlasenko wrote:
> 
> >-	bunzip_data *bd;
> > 	int i;
> >+	unsigned len;
> >
> > 	outbuf = xmalloc(IOBUF_SIZE);
> >-	i = start_bunzip(&bd, src_fd, NULL, 0);
> >-	if (!i) {
> >-		for (;;) {
> >-			i = read_bunzip(bd, outbuf, IOBUF_SIZE);
> >-			if (i <= 0) break;
> >-			if (i != full_write(dst_fd, outbuf, i)) {
> >-				i = RETVAL_SHORT_WRITE;
> >-				break;
> >+	len = 0;
> >+	while (1) { /* "Process one BZ... stream" loop */
> 
> I still viscerally cringe when I see while (1) instead of for(;;).  I'm aware 
> that modern optimizers take it out, but when there is a way to state exactly 
> what you want the code to do and you choose to instead say something you 
> _don't_ want the code to actually look like, I don't understand why.  Oh well.

I'm interested in this point, but can't find any arguments or discussion
on the for(;;) vs while(1) issue.  It is just a style preference, i.e., 
a religious issue?  I have seen for(;;) promoted over while(1), but why?

Just curious and trying to pick up on style and best practices,

Continued googling has yielded some results, and it seems to be historical
and not really very different in terms of speed or opcodes, though maybe
has some impact on diagnostics in some systems.

Some quotes from other googled pages:

  They are the same. There are much more important questions to ponder.

  More important, for (;;) is described in books as the preferred method
  for infinite loops.

  My new computer is so fast, it runs an infinite loop in just under
  an hour...

Ken


More information about the busybox mailing list