Thread: understanding while and for statement

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    Red face understanding while and for statement

    hi, i'm starting to learn to program with c++, using "C++ without fear book" and i'm on chapter 2. but as i keep trying to read over and over. Also research and looked at this website FAQ, i dont understand whats the difference. Is there a difference? or do they do the same thing? Only difference is that the "FOR" statement is cleaner and more organize? sorry if this is a stupid question.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, that is the only difference. These two loops are equivalent:

    Code:
    for(i=0;i<10;i++)
    {
    	...
    }
    Code:
    i=0;
    while(i<10)
    {
    	...
    	i++;
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Only difference is that the "FOR" statement is cleaner and more organize?
    That is the difference. The for statement is cleaner and more organized for certain tasks. For other looping needs, you don't need to follow the same pattern, so a while loop might be more appropriate.

    Making your code clear and organized is an important aspect to programming.

    BTW, a more descriptive example of equivalent loops is:
    Code:
    for(int i=0;i<10;++i)
    {
        ...
    }
    and
    Code:
    {
        int i=0;
        while(i<10)
        {
            ...
           ++i;
        }
    }
    Last edited by Daved; 05-03-2007 at 06:52 PM.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    3
    i c, thank you. =] *continue reading book*

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Another note, that book is not one I'd recommend for learning modern C++ programming techniques. You'll still learn a lot, but if you want to really program C++ you'll end up having to unlearn a few things. If you have the chance you might try a different book (e.g. Accelerated C++ or You Can Do It!).

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    3
    oh. thank you. i'll get that book too. =] hope i can catch up with everyone here.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    Yes, that is the only difference. These two loops are equivalent:
    Not quite. Consider what happens when you do a continue statement inside the loop.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Generally for statements are used when you know a process will repeat for a certain amount of time. A while loop is good if the condition for which the process is carried out can change during runtime. Lets say you want to do a fibbonacci sequence for up to 6 numbers, you would put the algorithm for the fibonnaci sequence in a for loop. Now lets say you want a menu to be displayed as long as the user doesn't press q. Then a while loop would be more more concise. You can make a for loop do what a while loop can do and vice versa, but either is more suited as far as syntax goes for different purposes.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    Not quite. Consider what happens when you do a continue statement inside the loop.
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int i;
    	
    	for(i=0;i<10;i++)
    	{
    		if(i == 5)
    		{
    			continue;
    		}
    		printf("i = &#37;d\n",i);
    	}
    	printf("\n");
    	
    	i=0;
    	while(i<10)
    	{
    		if(i == 5)
    		{
    			i++; /* Interesting line */ 
    			continue;
    		}
    		printf("i = %d\n",i);
    		i++;
    	}
    	
    	return 0;
    }
    When I tried the above without the marked line, the program would hang. When I added the marked line, program execution continued normally, and the two loops behaved the same.

    Commentary anyone?

    Edit: Apologies for writing this in C, but the idea is exactly the same, and nothing should be changed.
    Last edited by MacGyver; 05-04-2007 at 11:32 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would say that without that line, the continue brings control back to the top of the loop body... but i is still 5, so we have an infinite loop. With a for loop instead, the increment would still be performed despite the continue, so there will not be an infinite loop.
    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

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So basically, as long as the variable in a while loop is incremented manually before a continue, the two loops function the same, which is close enough to my statement that the two loops above were equivalent.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It was only a week ago that the same conversation came up:

    http://cboard.cprogramming.com/showt...852#post638852

Popular pages Recent additions subscribe to a feed