Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between while and do while C++? [duplicate]

I would like someone to explain the difference between a while and a do while in C++

I just started learning C++ and with this code I seem to get the same output:

int number =0;

while (number<10)
{
cout << number << endl;
number++
}

and this code:

int number=0;

do
{
cout << number << endl;
number++
} while (number<10);

The output is both the same in these both calculations. So there seem to be no difference. I tried to look for other examples but they looked way to difficult to understand since it contained mathemetical stuff and other things which I haven't quite learned yet. Also my book gives a sort of psychedelic answer to my question.

Is there an easier example to show the difference between these 2 loops?

I was quite curious

like image 644
Kahn Kah Avatar asked Aug 10 '14 21:08

Kahn Kah


People also ask

What is difference between while and do while loop in C?

while loop vs do-while loop in C. 1. While the loop is an entry control loop because firstly, the condition is checked, then the loop's body is executed. The do-while loop is an exit control loop because in this, first of all, the body of the loop is executed then the condition is checked true or false.

What is the difference between while and do while control statement in C?

Key Differences between while and do-while loop in CWhile loop checks the condition first and then executes the statement(s), whereas do while loop will execute the statement(s) at least once, then the condition is checked. While loop is entry controlled loop, whereas do while is exit controlled loop.

What is the difference between do while and while do?

do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example of Exit Control Loop.

What is the difference between while loop and do while loop with example?

The difference between while and do while loop is that in the while loop the condition is checked prior to executing any statements whereas in the case of do while loop, statements are run at least once, and then the condition is verified.


3 Answers

The while loop first evaluates number < 10 and then executes the body, until number < 10 is false.

The do-while loop, executes the body, and then evaluates number < 10, until number < 10 is false.

For example, this prints nothing:

int i = 11;

while( i < 10 )
{
    std::cout << i << std::endl;
    i++;
}

But this prints 11:

int j = 11;

do
{
    std::cout << j << std::endl;
    j++;
}
while( j < 10 );
like image 187
tillaert Avatar answered Nov 15 '22 12:11

tillaert


The while loop is an entry control loop, i.e. it first checks the condition in the while(condition){ ...body... } and then executes the body of the loop and keep looping and repeating the procedure until the condition is false.

The do while loop is an exit control loop, i.e. it checks the condition in the do{...body...}while(condition) after the body of the loop has been executed (the body in the do while loop will always be executed at least once) and then loops through the body again until the condition is found to be false.

Hope this helps :)

For Example: In case of while loop nothing gets printed in this situation as 1 is not less than 1, condition fails and loop exits

int n=1;
while(n<1)
    cout << "This does not get printed" << endl;

Whereas in case of do while the statement gets printed as it doesn't know anything about the condition right now until it executes the body atleast once and then it stop because condition fails.

int n=1;
do
   cout << "This one gets printed" << endl;
while(n<1);
like image 27
ahjashish Avatar answered Nov 15 '22 12:11

ahjashish


If you consider using a different starting value you can more clearly see the difference:

int number = 10;

while (number<10)
{
    cout << number << endl;
    number++
}
// no output

In the first example the condition immeditately fails, so the loop won't execute. However, because the condition isn't tested until after the loop code in the 2nd example, you'll get a single iteration.

int number = 10;

do
{
    cout << number << endl;
    number++
}
while (number<10);
// output: 10
like image 36
splrs Avatar answered Nov 15 '22 11:11

splrs