Thread: 'for' v. 'while' loop...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116

    'for' v. 'while' loop...

    ive been looking around the forums and i havent seen this one yet.
    is a for loop better to use then a while loop?
    are there situations when you would want to use one over the other?
    or is it all syntax?
    thanks in advance.
    ~M.I.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by MikeyIckey View Post
    ive been looking around the forums and i havent seen this one yet.
    Really? I thought it had been done to death.
    Quote Originally Posted by MikeyIckey View Post
    is a for loop better to use then a while loop?
    Yes. No. Use whichever fits the loop best. That is, if you are going from here to there, a for loop seems quite well suited for the task. If you are looping until some condition is met, a while loop may be better suited.
    Quote Originally Posted by MikeyIckey View Post
    are there situations when you would want to use one over the other?
    Preference to a degree. I'd say it mostly has to do with whether your loop needs an initial condition and/or specific "increment" or not.
    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
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    cool, thanks for the insight!

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Now you're gonna ask yourself, when should I use:
    Code:
    while ( condition )
    {
       ...
    }
    and when should I use:
    Code:
    do
    {
       ...
    } while ( condition )

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It used to be that for-loops where slightly better optimized (this is about 15 years ago, when compilers had to live in DOS where 640K was the limit, and non-PC machines has a few MB of RAM to share between different apps & users). In such a system, the compiler will not be able to do advanced optimizations that require building multiple large code-paths and check which is faster/better/shorter (whichever applies) - or if it did, it would make the compiler seriously slow, and whilst optimization is nice, having a compile that finishes the same day you started it is also useful.

    Nowadays, using a modern compiler, if you do the same work, you most likely end up with the same code, as long as you go about it in a reasonably similar way.

    Some "interesting" uses of for & while:

    Code:
        for(;;) ...    // same as while(1)
        
        for(p = head; p; p = p->next) ...  // Walk through a linked list from head to tail.  
        // Same code using while... 
        p = head;
        while(p)  {
            ... 
            p = p->next;
        }
        for(i = 0; i < 10; i++)  ... 
        // Same code using while using ugly coding style
        i = -1;
        while(i++, i < 10) ... 
        // Alternatively:
        i = 0; 
        while(i < 10) {
           ...
           i++;
        }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    thanks guys, i really appreciate the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM