Difference between loops?

Say I want to do something in a loop if something is true.
Is this:
1
2
3
while(a){ //assume a is a boolean value
dostuff();
}

different than this:
1
2
3
do{
dostuff();
}while(a)

I see no difference between these.
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
Ah.
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
Topic archived. No new replies allowed.