Thread: while loop vs for loop

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    while loop vs for loop

    Why we have "while" and "for" two different techniques for iteration? For do-while, we can use if we want to execute the loop once before checking condition. similarly is there any difference between while and for.

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krkr
    Why we have "while" and "for" two different techniques for iteration? (...) similarly is there any difference between while and for.
    The idea is that the different loop statements can help to convey the kind of flow of control that might be expected of the loop. For example, upon seeing a for loop, you would expect that there would be some kind of iteration over a range such that the iterator is incremented or otherwise points to an item further in the range until the range is exhausted, whereas with a while loop you might expect something more generic.

    Quote Originally Posted by krkr
    For do-while, we can use if we want to execute the loop once before checking condition.
    True, but you would need to either repeat the loop condition (violating the Don't Repeat Yourself principle), or you would need to pull out the loop condition into a function that can be called twice (good idea for a complex condition, but unnecessary hassle for a simple one).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    for (int counter = 0; counter < 10; ++counter) {
        if (counter == 0)  /* Skip first line for some reason.. */
            continue; /* Still calls increment above */
    }
    
    int counter = 0;
    while (counter < 10) {
        if (counter == 0)
            continue; /* Oops counter never increments because we call continue before the increment below */
        ++counter;
    }

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Originally Posted by laserlight
    For example, upon seeing a for loop, you would expect that there would be some kind of iteration over a range such that the iterator is incremented or otherwise points to an item further in the range until the range is exhausted, whereas with a while loop you might expect something more generic.
    means for better readability? Could you please provide sample code for this.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krkr
    means for better readability? Could you please provide sample code for this.
    You should have seen a great deal of such sample code by now. There's one provided by nonpuz in post #3, though I personally avoid the use of continue. You could easily write your own sample code, and if you cannot, then you should not be worrying about this to begin with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM