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.

What is Continue Statement in C?

The continue statement is used to skip the execution of the rest of the statement within the loop in the current iteration and transfer it to the next loop iteration. It can be used with all the C language loop constructs (while, do while, and for).

Continue Statement Syntax

The continue statement is used as per the following structure −

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

Continue Statement Flowchart

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.

Continue Statement with Nested Loops

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

Continue Statement Examples

Example: Continue Statement with While Loop

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>intmain(){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: Continue Statement with For Loop

The following program filters out all the vowels in a string −

#include <stdio.h>#include <string.h>intmain(){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]);}return0;}

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: Continue Statement with Nested Loops

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 ij, 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>intmain(){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);}}}return0;}

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: Removing Spaces Between Words in a String

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>intmain(){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]);}return0;}

Output

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

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

Example: Finding Prime Factors of a Number

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>intmain(){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;}return0;}

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *