Thread: Writing forever loops - using for(;;) or while(1)?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    Writing forever loops - using for(;;) or while(1)?

    Some people prefer using 'for (;;)', instead of 'while (1)', to write a loop that loops forever. Why?
    Code:
    for (;;) {
      DOSTUFF;
      if (...) exit(0);
    }
    Code:
    while (1) {
      DOSTUFF;
      if (...) exit(0);
    }
    Why is for(;;) better?
    Last edited by Hammer; 07-13-2005 at 04:22 PM. Reason: Disabled smilies to make it more readable [Hammer]

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here is a reply I had when this question had come up before.
    Quote Originally Posted by Dave Sinkula
    Hopefully your compiler would optimize the two to the same thing.
    Ok, I was hoping it would be that. But just in case, you know... like, if doing the while might require a check of some sort while the for would just loop without checking anything, maybe the for would be marginally more efficient or something. I'm about to grab a disassembler from somewhere though, I guess I'll have to see if there's any difference myself (even though I won't have a clue what's going on in the assembly :D)
    For what it's worth, I actually have encountered a case in which while(1) evaluated a condition rather than looping unconditionally. Granted, this was in C on an embedded platform nowhere near a PC. But after all, the code while(1) does specify a condition.

    I think the real point in this is why? Why write something that will hopefully be optimized away, rather than writing another way that will be correct no hoping required, no optimizations needed?

    I used to be a while(1) person. I have since a found reason to prefer for( ;; ).

    Also, my linter doesn't care for the while(1) and tells me wherever I have one: "test.c 5: [Info 716] while(1) ... ". And the following is the message description:
    716 while(1) ... -- A construct of the form while(1) ... was found. Whereas this represents a constant in a context expecting a Boolean, it may reflect a programming policy whereby infinite loops are prefixed with this construct. Hence it is given a separate number and has been placed in the informational category. The more conventional form of infinite loop prefix is for(;;)
    Last edited by Dave_Sinkula; 07-13-2005 at 03:38 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    "while (1)" is not readable.

    But "(;;)" is secret code for the word, "ever".

    :-)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Also, fussy checkers like lint will complain that while(1) is an expression with a constant answer. More often than not, you want your conditions to be actually conditional on something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    it is a matter of style.
    BSD style recommends using for (;

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    I have never come across a situation where I need an infinite loop. I believe that you always need some kind of 'natural' control over it instead of
    Code:
    for (;;) {
      if () exit(0);
    }

  7. #7
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by sand_man
    I have never come across a situation where I need an infinite loop. I believe that you always need some kind of 'natural' control over it instead of
    Code:
    for (;;) {
      if () exit(0);
    }
    I disagree. How would you recommend drawing a loop of this form:

    Code:
    top:
        /* preliminary operations that also get repeated */
    
        if (/* blah */ ) goto end;
    
        /* Further operations */
    
        goto top;
    end:
    You could write it like:

    Code:
    /* preliminary operations */
    
    while (! /* blah */) {
    
        /* Further operations */
    
        /* preliminary operations COPIED and PASTED! */
    
    }
    But much better is simply:

    Code:
    for (;;) {
    
        /* preliminary operations */
    
        if (/* blah */) break;
    
        /* Further operations */
    
    }

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    What is wrong with using a flag to continue looping?

    Code:
    top:
        /* preliminary operations that also get repeated */
    
        if (/* blah */ ) goto end;
    
        /* Further operations */
    
        goto top;
    end:
    is the same as:

    Code:
    int f = 1;
    while(f){
    
      /*do stuff*/
      
      if(/*you want to leave loop*/){
        f = 0;
        continue;
      }  
    
      /*do other stuff*/
    }
    I know my code does extra condition checks but...
    Last edited by sand_man; 07-15-2005 at 08:12 PM.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    There are state machines. And embedded systems which run "forever".
    Last edited by Dave_Sinkula; 07-16-2005 at 03:51 PM. Reason: Removed misplaced reply ('continue statement' thread).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing encrypted value to file.
    By mkthnx001 in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 12:42 PM
  2. Optimize file writing
    By Opariti in forum Windows Programming
    Replies: 7
    Last Post: 10-23-2008, 01:32 PM
  3. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  4. writing to a binary file
    By Inver28 in forum C Programming
    Replies: 4
    Last Post: 03-02-2008, 02:36 AM
  5. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM