Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Saturday, 16 July 2011

The do while loop

Do while:
                As you know if the conditional expression controlling a while loop initially false then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a while loop at least once, even if the conditional expression is false at the beginning. We can say that, there are times while programming with java when you would like to test the conditional expression at the end of the body of loop rather than at the beginning. Fortunately, java supplies a loop that does just that: the do while.

The do while loop always executes its body first (even if the conditional expression is false to begin with) and then checks the conditional expression. It does so because its conditional expression is at the bottom/end of the body of the loop. Its general form is:

do
{
//body of loop
}
while (condition);

Each iteration of this loop first executes body of the loop and then evaluates the conditional expression, controlling the loop. If this condition is true then loop will repeat otherwise, the loop terminates. Remember, a condition must be a Boolean expression.

No comments:

Post a Comment