Click here to Skip to main content
15,920,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what is while and do while
[2nd Question Removed, already on Site]
Posted
Updated 13-Aug-13 20:36pm
v2

while and do...while - loops with one most important difference. To find the difference, please, see the documentation:
while Statement (C)[^]
do-while Statement (C)[^]
 
Share this answer
 
Comments
CPallini 14-Aug-13 3:21am    
5. RTFM is quite effective, indeed :-)
Maciej Los 14-Aug-13 3:29am    
Thank you, Carlo ;)
These are two types of control flow techniques in which we achieve looping through a number of items.There is a small difference in while and do while.

-> In while you can only get inside body of loop if you satisfy the provided condition.If your condition falsifies at first attempt,then you can't go inside of loop.
-> In Do while whether your condition falsify or not,but you can get inside loop atleast once.Afterwards condition is checked for each iterations.For first iteration it does not check the given condition.

This is a minor but very remarkable difference between while and do while.
 
Share this answer
 
The description given by @Jitendra Sabat is what exactly while and do while perform and is optimum solution

One real time example i can give of while and do while are

1)While
When we visit shopping malls they first check us thoroughly and if all is fine then we can make our entry in. Similarly in C or any other programming language "while" first checks for condition and if all is fine only then the code inside the loop can execute.
ex.
C++
int i = 6
while(i<5)
{
    //Wont execute because condition fails, 6 is not less than 5.
    printf("incrimenting i");
}


2)do while
When we visit shops inside shopping mall first we buy all the articles and then we are coming out the security person checks our bills and contents if all is fine. Similarly "dowhile" will first let you execute your code and at last it checks for conditions. In "dowhile" no matter what the code executes at least once as condition is checked at the last.
ex.
C++
int i = 6
do
{
    //Executes once as no condition is checked prior to logic.
    printf("incrimenting i");
}while(i<5)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900