View Poll Results: Uhh...vote

Voters
20. You may not vote on this poll
  • do-while

    4 20.00%
  • while

    13 65.00%
  • I never use any of these

    1 5.00%
  • Who cares

    2 10.00%

Thread: do-while vs while

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    do-while vs while

    I don't get it.....

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int x = 0, y = 0;
    
    	while(x < 10)
    	{
    		x++;
    		cout << x << " ";
    	}
    
    	cout << endl;
    
    	do
    	{
    		y++;
    		cout << y << " ";
    	}
                   while(y < 10);
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    the only difference is that a do-while loop executes the loop at least ONCE, before even checking if the condition is true or false. a while loop will not loop at all unless the condition is true.

    Under most circumstances you can use them interchangeably, but there are some instances when a do-while is necessary over a while.

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669
    CaptainPenguin-but there are some instances when a do-while is necessary over a while.


    and vise versu... It depends on what kind of flow you want in your program.

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    A 'while' loop will first check the condition (x < 10), and then execute the block. A 'do while' loop will first run the block, and then test the condition

    So you've got the subtle difference of: "test-execute" / "execute-test"

  5. #5
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    I personaly choose "do while". But as mentioned earlier, It depends on what type of flow you want

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    you need a choice for both. I use both depending on what I need to get done.

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    It's not which one to choose....
    Choose the one you need.
    Use the do while loop when you want your statements after do to be executed at least once, and use the other one to force the check before executing the statements.

    I don't get the point of the poll it's same as the '\n' VS. endl
    I think that you should use what you need depending on the case.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Why not start a poll: "which keyword is best, 'class' or 'int'?"
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    lol, like your post Sang-drax, actually that's true maybe you'll find one soon!

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Do-while is never necessary; you can just copy the contents of the inside of the loop to before the loop
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    do while vs while is not something you vote on. each loop does slightly different things and you use whatever is proper for your situation. sheeesh.
    hello, internet!

  12. #12
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    for things such as a game loop i perfer while, example:

    Code:
    cout<<"Enter a letter";
    cin>>letter
    
    while (letter != 'Q' || 'q')
    {
    .......
    .......
    .......
    }

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    do

    I always use
    While loops because it is easier to read through the code for me.. and it solves just about every while loop problem i come across.. however i will use a
    do. while when i am inputing from a file to use as a priming read.. saves me from having to repeat the same code

    Code:
    do
    {
      infile >> bob;
    }
    while(!infile.eof());

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Correct me if I'm wrong, but if you're reading from a file, shouldn't you check for eof right away (i.e. instead of after the loop)? I mean, if the file's empty, the first iteration of the do-while will have been a waste of cpu time, wouldn't it?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    is it? honestly i cannot remember.. so long ago that i actually had to read from files.. for some reason i was thinkin' that u had to do a priming read before you could check the eof() status.. guess i was wrong :P then i guess i never use a do..while loop..

Popular pages Recent additions subscribe to a feed