The do – while loop:
The
while loop, makes a test of condition before the loop is executed. Therefore,
the body of the loop may not be executed at all if the condition is not
satisfied at the very first attempt. 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 do statement. General Syntax for do – while loop
is:
Initialization;
do
{
body of
the loop;
increment
/ decrement;
}while ( condition );
On
reaching the do statement, the program proceeds to evaluate the body of the
loop 1st. 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 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.
Post a Comment