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; }
do-while
while
I never use any of these
Who cares
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.
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.
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.
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"
I personaly choose "do while". But as mentioned earlier, It depends on what type of flow you want
you need a choice for both. I use both depending on what I need to get done.
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.
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
lol, like your post Sang-drax, actually that's true maybe you'll find one soon!
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.
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!
for things such as a game loop i perfer while, example:
Code:cout<<"Enter a letter"; cin>>letter while (letter != 'Q' || 'q') { ....... ....... ....... }
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());
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.
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..