Thursday, 7 October 2010

25. DO.....WHILE LOOP

Do....while loop is similar to the while loop. While loop checks the condition first then the statements are executed but the do…..while loop checks the conditions after the statement are executed. This means that do…while loop executes the statements at least once, while the while loop might not execute the statements at all based on its condition.

The expression must have arithmetic or pointer type. Execution proceeds as follows:
1.   The statement body is executed.
2.   Next, expression is evaluated. If expression is false, the do-while statement terminates and control passes to the next statement in the program. If expression is true , the process is repeated, beginning with step 1.

Example of the do-while statement:
do
{
    y = f( x );
    x--;
} while ( x > 0 );

In this program, the two statements y = f( x ); and x--; are executed, regardless of the initial value of x. Then check the condition (x>0). If the condition is true then the statement body is executed again and x > 0 is reevaluated. The statement body is executed repeatedly as long as the condition is true. When the condition is false then do…..while loop is stop.
 post by Arnob

No comments:

Post a Comment