Tuesday 14 December 2010

difference between while and do while loop

The difference is in when the condition gets evaluated. In a do..while loop, the condition is not evaluated until the end of each loop. That means that a do..while loop will always run at least once. In a while loop, the condition is evaluated at the start.

Do while loop will be executed at least once.......but while loop will check the condition first and then it may or may not get executed depending on the condition.

In your example wdlen may assume any garbage value which is > 2 so while loop will never get executed.

whereas do while loop will be executed and will tell u to enter the value and check that value in terminating condition


Here I assume that wdlen is evaluating to false (i.e., it's bigger than 1) at the beginning of the while loop, so the while loop never runs. In the do..while loop, it isn't checked until the end of the first loop, so you get the result you expect.

Statements
repetative statements



No comments:

Post a Comment