DO - WHILE STATEMENT
In the while loop, the condition is tested at the entry level.if the condition fails at very first time, the body of the loop will not be executed at all.In case of do_while statement , the condition is tested at the exit level and hence the body of the loop is executed at least once whether the condition is true or false.At the end of the do_while loop,the condition is tested and if it is true, the loop gets executed once again.This process continues as long as the test condition is true.When the test condition becomes false, the loop is terminated and the control is transferred to the statement immediately after the do_while statement .eg:
x=14;
do
{
y=x+2;
x- -;
}while(x>0);
In this do_while statement , the two statement y=x+2; and x- - are executed regardless of the initial value of x.Then x>0 is evaluated.If x is greater than 0, the body of the loop is executed again and the condition x>0 is reevaluated.The body of the loop is executed repeatedly as long as x remains greater than 0.Execution of the do_while statement terminates when x becomes 0 or negative.The body of the loop is executed at least once.
No comments:
Post a Comment