Continue Statement in C



The behaviour of continue statement in C is somewhat opposite to the break statement. Instead of forcing termination of a loop, it forces the next iteration of the loop to take place, skipping the rest of the statements in the current iteration.

The continue statement is used as per the following structure:

while (expr){
   . . .
   . . .
   if (condition)
      continue;
   . . .
}

The following flowchart represents how continue works:

switch statement in C

You must use continue keyword inside a loop. Use of continue outside loop will result in compilation error. Unlike the break statement, continue is not used with switch…case statement.

In case of nested loop, continue will continue next iteration of nearest loop.

Do not confuse that continue transfer program control to loop condition. It transfers program control to next iteration of the loop.

Use of continue statement is often used with if statements.

Example

#include <stdio.h>

int main(){
   int i=0;

   while (i<10){
      i++;
      if (i%2 == 0)
         continue;

      printf("i: %d\n", i);

   }
}

Output

i: 1
i: 3
i: 5
i: 7
i: 9

In this program the loop generates 1 to 10 values of the variable i. Whenever it is an even number, the next iteration starts, skipping the printf() statement. Only the odd numbers are printed.

Example

The program below filters out all the vowels in a string.

#include <stdio.h>
#include <string.h>

int main () {

   char string[] = "Welcome to TutorialsPoint C Tutorial";
   int len = strlen(string);
   int i;

   printf("Given string: %s\n", string);
   printf("after removing the vowels\n");

   for (i=0; i<len; i++){
      if (string[i]=='a' || string[i]=='e' || string[i] == 'i' || string[i] == 'o' || string[i] == 'u')
         continue;
      printf("%c", string[i]);
   }


   return 0;
}

Output

Given string: Welcome to TutorialsPoint C Tutorial
after removing the vowels
Wlcm t TtrlsPnt C Ttrl

Example

The code below detects the blank space between words in a string, and prints each word in different line

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main () {

   char string[] = "Welcome to TutorialsPoint C Tutorial";
   int len = strlen(string);
   int i;

   printf("Given string: %s\n", string);

   for (i=0; i<len; i++){
      if (string[i]==' '){
         printf("\n");
            continue;
      }
      printf("%c", string[i]);
   }

   return 0;
}

Output

Given string: Welcome to TutorialsPoint C Tutorial
Welcome
to
TutorialsPoint
C
Tutorial

Example

If a continue statement appears inside an inner loop, the program control jumps to the beginning of the corresponding loop.

In the example below, there are three for loops one inside the other. These loops are controlled by the variables I, j and k respectively. The innermost loop skips the printf statement if j has equal to either I and j, and goes to its next value of k. The second j loop executes continue when it equals i. As a result, all the unique combinations of three digits 1, 2 and 3 are displayed.

#include <stdio.h>

int main () {

   int i, j, k;
        
   for(i=1;i<=3;i++){
      for(j=1;j<=3;j++){
         if (i==j)
            continue;
         for (k=1; k<=3; k++){
            if (k==j || k==i)
               continue;
            printf("%d %d %d \n", i,j,k);

         }

      }
   }
   return 0;
}

Output

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Example

One of the cases where continue statement proves very effective is in the problem of writing a program to find prime factors of a given number.

The algorithm of this program works like this:

The given number is successively divided by numbers starting with 2.

If the number is divisible, the given number is reduced to the division, and the resultant number is checked for divisibility with 2 till it is no longer divisible.

If not by 2, the process is repeated for all the odd numbers starting with 3

The loop runs while the given number reduces to 1.

Here’s the program to find the prime factors:

#include <stdio.h>

int main (){
   int n = 64;
   int i, m=2;

   printf("Prime factors of %d: \n", n);

   while (n>1){
      if (n%m==0){
         n = n/m;
         printf("%d ", m);
         continue;
      }
      if (m==2)
         m++;
      else
         m=m+2;
   }

   return 0;
}

Output

Prime factors of 64: 
2 2 2 2 2 2 
c_loops.htm
Advertisements