C continue Statement

In this tutorial, you will learn how to use continue statements in the C programming language with the help of examples.


The continue statement in the C programming language brings the control to the beginning of the loop. It skips some lines of the code inside the loop and continues for the next iteration. The continue statement mainly used to skip some part of the loop if the specified condition is satisfied.

Syntax

The syntax of the C continue is as follows:

//loop statements 
continue; 
//some lines of the code which is to be skipped

Example

Let’s check a simple example of the C continue statement.

#include <stdio.h>
int main()
{
    int i = 1; //initializing a local variable
    //starting a loop from 1 to 10
    for (i = 1; i <= 10; i++)
    {
        if (i == 4)
        { //if value of i is equal to 4, it will continue the loop
            continue;
        }
        printf("%d \n", i);
    } //end of for loop
    return 0;
}

Output

1
2
3
5
6
7
8
9
10
You can see from the above example that the value 4 is not printed because once the condition if (i == 4) is satisfied the loop continues for the next iteration without printing the value.

Example 2: continue statement

Let’s check another example of the C continue statement.

#include<stdio.h>  
void main ()  
{  
    int i = 1;   
    while(i<=10)  
    {  
        printf("%d\n", i);  
        continue;   
        i++;  
    }  
}

Output

1
1
1
1
ctrl+c
The above program will run forever i.e. an infinite loop because we have place a continue; statement before updation of the loop variable.

C continue Statement with Nested Loop

In this case, only the innermost loop will continue. There is no effect on the outer loop. Let’s check an example:
#include <stdio.h>
int main()
{
    for (int i = 1; i <= 5; i++)
    {
        for (int j = i; j <= 5; j++)
        {         

            if(i==2){
                continue;
            } // will continue the loop of j only
            printf("%d\t", j);
        }
        printf("\n");
    }
    return 0;
}

Output

1   2   3   4   5
3   4   5
4   5
5

From the above example, you can see that in the second iteration the value 2 3 4 5 not printed to the console due to the continue statement in the inner loop with the condition if(i==2).

Please get connected & share!

Advertisement