Monday, December 14, 2009

4.8 THE continue STATEMENT

The continue statement is used to bypass the remainder of the current pass through a loop.The loop does not terminate when a continue statement is encountered.Rather the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop.(Note the distinction between continue and break.)

The continue statement can be included within a while,a do-while or a for statement.It is written simply as

continue;

without any embedded statements or expressions.

Here are some illustrations of  loops that contain continue statements.
First,consider a do-while loop.

do {
scanf ("%f", &x);
if (x < 0) {
printf ("Error -Negative value for x");
continue;
}
/*process of non negative value of x*/
.....................

}while (x <=100);

Here is a similar for loop.

for(count=1; x <= 100; ++count) {

scanf ("%f", &x);
if (x < 0) {
printf ("Error -Negative value for x");
continue;
}
/*process of non negative value of x*/  
........................
}

In each case, the  processing of the current value of x will be bypassed if the value of x is negative.Execution of the loop will then continue with the next pass.

No comments:

Post a Comment