for loop or while loop

This is a discussion on for loop or while loop within the C++ Programming forums, part of the General Programming Boards category; last question that I am not sure how to answer on my study guide: When should a while loop be ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    18

    Post for loop or while loop

    last question that I am not sure how to answer on my study guide:

    When should a while loop be used and when should a for loop be used?

    In my understanding, they basically do the exact same thing, and I can pretty much write either type of loop to do the exact same thing... I guess, is it just preferential to use a for loop when you only want the loop to go through a certain number of iterations, and you should use a while loop when you just want a particuliar condition to be met for the loop to keep running?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Well I guess it's just a matter of preferance, since a while loop can do almost anything a for loop can. All you have to do is:
    Code:
    while (count < 6)
    {
    count++
    //Blah Blah Blah...
    }
    And that would work just as well as a for loop.. wouldn't it? I'f I'm wrong could somebody please correct me.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Really it's just a matter of preference.

    For loops handle: initialize, condition, post-assignment(usually incrementing)

    While loops just handle condition and often post-assignment.


    int i;

    for( i = 0; i < 10; i++)
    {
    //...do
    }


    int i = 0;

    while(i < 10)
    {
    //...do
    i++;
    }


    Sometimes the incrementation takes place in the condition part of the while loop, but be careful, often the incrementation takes place first so that:

    int i = 0;

    while(i++ < 10) //...i = 1 before entering loop! As if ++i !!
    {
    //...do
    }

    ...must be rewritten:

    int i = -1;

    while(i++ < 10) //...i = 0 before entering loop! As if ++i .
    {
    //...do
    }


    ...this may depend on your compiler, though...

    Often the incrementation is done on a variable being worked on, and the above problem does not occur:

    int variable[10];
    int i = 0;

    while(i < 10)
    {
    variable[i++] = i; //...no problem
    }



    Incidentally, a while loop written as a for loop would be:



    int i = -1; //...to avoid side-effect problem...

    for( ; i++ < 10; )
    {
    //...do
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: for loop or while loop

    Originally posted by slamit93
    last question that I am not sure how to answer on my study guide:

    When should a while loop be used and when should a for loop be used?

    In my understanding, they basically do the exact same thing, and I can pretty much write either type of loop to do the exact same thing... I guess, is it just preferential to use a for loop when you only want the loop to go through a certain number of iterations, and you should use a while loop when you just want a particuliar condition to be met for the loop to keep running?
    If you are going to iterate a known number of times, you should use a for loop.

    If you don't know how many times you are going to loop, but know that it is at least 1, you should use a do-while loop.

    If you don't know how many times you are going to loop, and it might not even be 1 loop, you should use a while loop.

    As has been pointed out, you could use one 'loop-type' to do what the other do with a little tweaking and clever layout, but these are the general purposes.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

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-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21