continue statement in C


Reading time: 30 minutes

The continue statement is a jump statement in C. It is another one of the jump statements like the break statement as both the statements skip over a part of the code. But the continue statement is somewhat different from the break statement.
Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.

Syntax: continue;

continue

Working of continue statement:

**while loop**
while(expression)
{
  statement1;
  if(condition)
    continue;
  statement2;
 }
 statement3;

**for loop**
for(int;expression;update)
{
  statement1;
  if(condition)
    continue;
  statement2;
 }
 statement3;

**do-while loop**
do
{
  statement1;
  if(condition)
    continue;
  statement2;
 }while(expression);
 statement3;
  • For the for loop continue causes the next iteration by updating the variable and then causing the test-expression's evaluation.
  • For the while and do-while loops the program control passes to the conditional tests.

Do not confuse the break and continue statements

  • A break statement inside a loop will abort the loop and tranfer control to the staement following the loop.
  • A continue statement will just abandon the current iteration and let the loop start the next iteration.

Following example establishes the difference between the two:-

#include<iostream.h>
int main()
{
    int i;
    printf("The loop with break produces output as:\n");
    for(i=1;i<=10;++i)
    {
          if((i%3)==0)
           break;
          else
            printf("%d",i);
     }
     printf("The loop with continue produces output as:\n")
     for(i=1;i<=10;++i)
     {
          if((i%3)==0)
            continue;
           else
          printf("%d",i);
     }
     return 0;
 }
Output

The loop with break produces output as:

1
2

The loop with continue produces output as:

1
2
4
5
7
8
10

Though, continue is one prominent jump statement, however, experts discourage its use whenever possible.Rather,code can be made more comprehensible by the appropriate use of if/else.

Question

continue statement

exit
entry
both
none
Since the test expression is checked at the end of the loop, therefore it is an exit controlled loop. In case of while loop the condition is checked before entering the loop, and hence it is an entry controlled loop.