Continue Statement in C

Continue Statement in C :

  • continue statement in c is a jump statement which skips over a part of the code just like a break statement.
  • The continue statement is somewhat different from break as execution of this statement does not terminate from the loop but  it suspend the next iteration and forces the next iteration of the loop to take place.
  • It is mostly used inside loops.

Syntax for continue Statement in C

continue;

General Flow Diagram of continue Statement in C:

continue statement in C - General

Working of Continue Statement in loops:

The following figure explain the working of continue statement in loops :

Continue Statement in C - while Continue Statement in C - do while
Continue Statement in C - for

Note : The continue statement keep the rest of the loop statements and causes the next iteration in the for, while and do while loop. Now Let’s check where the control of the program will reach after occurrence of a continue statement in loops.

  • For the while loop, the continue statement skips the rest of the statements and the program control process to conditional tests.
  • Similarly, in the do while loop, the continue statement skips the rest of the statement to execute and the control of the program will reach/process to conditional tests.
  • But for the for loop, continue causes the next iteration by updating the variable and then causing the test expressions evaluation.

Example of continue Statement in for loop :

//program to demonstrate the working of continue statement in C programming - for Loop
# include <stdio.h>
int main()
 {
  int a,b,c,i;
  for(i=0;i<2;++i)
    {
     printf("Enter numerator and denominator):");
     scanf("%d %d ",&a, &b);
     if(b==0)
       {
        printf("\nThe denominator cannot be zero \nEnter again !!\n");
        continue; /*This statement causes the next iteration by
                   updating i to i+1 and then test the
                   expression (i<2)*/
       }
    else 
      {
       c=a/b;
       printf("\n Quotient = %d\n",c);
      }
   }
 return 0;
}
Output :
Enter numerator and denominator : 5 0
The denominator cannot be zero
Enter again !!
Enter numerator and denominator : 10 5
Quotient = 2

Example of continue Statement in C Programming – while loop :

//program to demonstrate the working of continue statement in C programming - while Loop
# include <stdio.h>
int main()
 {
  int counter=10;
  while (counter >=0) 
    {
     if (counter == 4)
       {
        counter--;
        continue;
       }
     printf("%d  ", counter);
     counter--;
    }
   return 0;
  }
Output :
10 9 8 7 6 5 3 2 1

Example of continue Statement in C Programming – do while loop :

//program to demonstrate the working of continue statement in C programming - do while Loop
#include <stdio.h>
int main () 
  {
   int a = 10;
   do {   
       if( a == 15) 
         {
         a = a + 1;
         continue;    // skip the iteration
         }
       printf("value of a: %d\n", a);
       a++;
      } while( a < 20 );
   return 0;
  }
Output :
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Difference between break and continue Statement :

Do not confuse between the break and continue statement. Check out the differences between the two and clear your concept :

Basis break statement continue statement
Execution of Next Statement The execution of the next statement after the break statement will be that statement which is outside the block. The execution of the next statement after the continue statement will be next iteration of the loop.
Repetition When a break statement is executed in a loop, the repetition of the loop will be terminated. A continue statement will just abandon the current iteration and let the loop start the next iteration.
Keyword The keyword is break; The keyword is continue;
Used in break statement is used in loops, if-else and switch statement. continue statement is used only within loop. You will get an error if this appears in switch statement.
What they are used for : break statement is used to transfer the control outside the loop/switch-case/if-else. continue statement transfer the control within the loops but to the next iteration of it.

Following example program uses two loops to perform the same thing, but replace break statement with continue. Have a look at one code and then the output to understand the difference between break and continue statement in C.

// Program to illustrate the difference between the break and continue statements.
#include<stdio.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\n",i);
       }
    printf("The loop with '\continue\' produces output as : \n");
    for(i=1;i<=10;++i)
       {
         if(i%3 == 0)
              continue;
         else
              printf("%d\n",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
     

Incoming search terms:

  • pdf do while do while loop break and continue statement in c download
  • continue statement in c pdf
  • difference between break and continue statement in c with example pdf file

Leave a Reply