In the second, dostuff() will be executed at least once, regardless of whether "a" is true or not. This is the only difference here.
A little more raw explanation:
while() - check the condition, and if true, execute the code block. Repeat.
do...while() - execute the code block, check the condition, and if true, repeat.
Last edited on
Like someone else said, it is possible for the
while
loop to run 0 times, the
do while
loop will run at least once.
I use it for things like
1 2 3 4 5 6 7 8
|
char input;
do{
PlayGame();
do{
cout << "Play Again? y/n" << endl;
cin >> input;
while(input != 'y' && input != 'n');
while(input == 'y');
|
This could be done using
while
loops but that would require you to initialize input to 'y' before you enter the loop.
Last edited on