I just wondered

closed account (9wX36Up4)
what is the different between Do/While and While? Because Do/While is Doing something while the condition is true. While is doing something while the condition is true. ??????? I m confused.
do/while is guaranteed to "run" at least once, but while will only "run" if the condition is already true when it comes to the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13

bool run = false;

do
{
    cout << "you can see this";
}
while (run);

while (run)
{
    cout << "but not this";
}
Last edited on
They are exactly the same except that do while tests the condition after it runs through the block of statements so its going loop atleast once. While tests the condition before it runs through the statements.

You will realize that you will hardly ever use do-while. Just focus on the while loop.
Last edited on
Do-while is useful for, say, running a program and asking the user if they want to run it again. ;)
closed account (9wX36Up4)
thank u all i understand now the difference between them
Do-while can be used if you need to test condition for a variable that's to be initialized in the loop. Example :
1
2
3
4
5
6
int x;
do
{
cin>>x
.....
}while (x != 0);
Topic archived. No new replies allowed.