• C Programming Video Tutorials

Continue Statement in C



The behaviour of continue statement in C is somewhat opposite to the break statement. Instead of forcing the 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 the continue statement inside a loop. If you use a continue statement outside a loop, then it will result in compilation error. Unlike the break statement, continue is not used with the switch-case statement.

In case of nested loops, continue will continue the next iteration of the nearest loop. The continue statement is often used with if statements.

Example 1

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.

#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

Example 2

The following program 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

Run the code and check its output −

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

Example 3

The following code detects the blankspaces between the words in a string, and prints each word on a 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

On executing this code, you will get the following output −

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

Example 4

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 k is equal to either i or j, and goes to its next value of k. The second j loop executes the 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

Run the code and check its output −

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

Example 5

One of the cases where the 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 until 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

Here, the given number is 64. So, when you run this code, it will produce the following output −

Prime factors of 64:
2 2 2 2 2 2

Change the number to 45 and then 90. Run the code again. Now you will get the following outputs −

Prime factors of 45:
3 3 5

Prime factors of 90:
2 3 3 5
c_loops.htm
Advertisements