Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what is the difference between "do while" and "while"?

+1 vote
asked Nov 27, 2018 by Ahmad Raza (130 points)

3 Answers

0 votes
answered Nov 28, 2018 by Юрий Гуреев (1,100 points)

The do...while loop is a variant of the while loop with one important difference. The body of do...while loop is executed once before the test expression is checked.

do {
   // codes;
}
while (testExpression);

0 votes
answered Nov 28, 2018 by Akhila Mekapothula (460 points)
In while loop we check the condition at begininig i.e while entering into loop stataements

syntax:

while(condition)

{

statements;

}

in dowhile loop we check the condition at end of loop.By default without checking condition it executes one time body of loop

syntax:

do

{

statements;

}while(condition);
0 votes
answered Jan 17, 2019 by Jyothi_Rk
edited Jan 22, 2019
While: Condition is checked first ,only if it is satisfied/true then the body is executed.[ Min Num of time the body executes is 0].

Do While: Initially body is executed and later the condition is checked. This means even if the condition is false, body will execute for 1 time. [ Min Num of time the body executes is 1] .Only when the body has to be executed atleast once,then use this condition.

You can imagine this using simple example,

1]While(cake ==veg)

{

    eat the cake;

}

2]do

{

   eat the cake;

}while(cake == veg);

If the cake is non veg then using  "do while" will make you to eat the cake @1st and later for next bite ,it will check the condition.Where as "While" will check for the1st bite and if the cake is non veg then it will not make you to eat the cake.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...