The do statement
The do statement
In while loop the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. But on some occasions, it might be necessary to execute the body of the loop before the test is performed. Such situations can be handled with the help of the do statement.
On reaching the do statement, the program proceeds to evaluate the body of the loop first. At the end of the loop, the test condition in the while statement is evaluated. If the condition is true , the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true. When the condition becomes false, the loop will be terminated and the control goes to the statement that appears immediately after the while statement .
Do while provides an exit controlled loop.
Program illustration of do while loop
Class do while
{
Public static void main(string args{})
{
Introw.column.y;
System.out.println( Multiplication table ):
Row = 1;
Do
{
Column = 1 ;
Do
{
Y = row * columnd ;
System.out.print(“ “ + y);
Column = column + 1 ; ;
}
While ( column< = 3)
System.out.println(“ ”);
Row = row + 1 ;
}
While ( row<=3);
}
}