Loading, please wait...

Continue statement

The continue statement when executed in a while, for or do…while statement skips the remaining statements in the body of that control statement and performs the next iteration of the loop.

In while and do…while statements, the loop-continuation test is evaluated immediately after the continue statement is executed. In the for the statement, the increment expression is executed, then the loop-continuation test is evaluated.

Earlier, we said that the while statement could be used in most cases to represent them in the statement. The one exception occurs when the increment expression in the while statement follows the continue statement. In this case, the increment is not executed before the repetition continuation condition is tested, and the while does not execute in the same manner as the for. The example uses the continue statement in a for a statement to skip the printf statement and begin the next iteration of the loop.

 

Example

./*Using the continue statement in a for statement*/
#include <stdio.h>

int main(void)
{
int x;

/* loop 10 times */

for ( x = 1; x <= 10; x++ ) {
/* if x is 5, continue with next iteration of loop */
if ( x == 5 ) {
continue;
} /* end if */

printf( "%d ", x ); /* display value of x */
} /* end for */

printf( "\nUsed continue to skip printing the value 5\n" );
return 0;
}

 

Output

1 2 3 4 6 7 8 9 10

Used continue to skip printing the value 5 Some programmers feel that break and continue violate the norms of structured programming. The effects of these statements can be achieved by structured programming techniques.