The continue Statement
In some programming situations we want to take the control to the beginning of the loop. The keyword "continue" allows us to do this, when the keywords continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is usually associated with an if condition. The continue statement tells the compiler, skips the following statements in the loop and continue with the next iteration.
In the while and do-while loops, continue causes the control to go directly to the test conditional expression and then to continue the iteration. In for loop, the continue transfer the control to the increment/decrement clause of the loop before the test conditional expression.
while(conditional expression)
{
statement;
statement;
if(condition)
continue; //Continue with next iteration (while Loop start again....)
statement;
statement;
}
statement;
statement;
for(initialize clause; conditional expression; increment/decrement clause)
{
statement;
statement;
if(condition)
continue; // Continue with next iteration (for loop start again)
statement;
statement;
}
statement;
statement;
do
{
statement;
statement;
if(condition)
continue; // Continue with next iteration ( start from while condition)
statement;
statement;
} while(condition);
statement;
statement;
for(initialize clause; conditional expression; increment/decrement clause)
{
for(initialize clause; conditional expression; increment/decrement clause)
{
statement;
statement;
if(condition)
continue; // Continue with next iteration (Inner for loop start again)
statement;
statement;
}
statement;
statement;
}
statement;
Example:
void main ( )
{
int n = 1;
for (; n < 10; n++)
{
if (n ==3)
continue;
printf("number = %d \n", n);
}
}