What a continue statement can do?

Continue statement in C is used to control the flow of program. It brings the program control to the beginning of the loop by skipping some code bellow it. Continue is a keyword in C which can bring the loop to the starting point of the loop. We can use this keyword to control our program flow inside any loop as well as nested loop. Here we will see the use and function of this continue keywords with C language.

Use and syntax

Continue statement is also used for bringing the loop to its beginning. When we use this statement inside C program it will not execute the instruction bellow it inside of that loop. It is very necessary to know the function of continue keyword as it is used in many program to control its output.

The syntax of continue statement is as follows;

// Condition when needs to break
continue;
// skip code

Use of continue statement with for loop in C

In this program we will use continue statement to continue the loop by skipping some code for a specific condition. When the compiler gets this statement, it goes to the beginning of the loop without executing the code bellow continue keyword. So, let’s see a program where we will use the continue statement with for loop in C program.

// use of continue statement with for loop in C
#include <stdio.h>
int main(){
    int i;
    for (i = 1; i <= 10; i++){
        if (i == 4){
            continue;     // 4 will not print here
        }else{
            printf("%d\n", i);
        }
    }
return 0;
}

The above program should print all the integers from 1 to 10. But here we have seen that if the value of i == 4, then the program will skip printing that value which have instructed inside else block. For this we will see the output of all integers from 1 to 10 without 4. See bellow how the output will look like.

use of continue statement in c

While loop & continue statement

We can also use continue statement with while loop as like as for loop. See the example program bellow  to get an idea about use of this statement with while loop in C programming. You will learn a lot of programming idea in our C programming chapter.

// use of continue statement with while loop in C
#include <stdio.h>
int main(){
    int j = 1;
    while (j <= 10){
        if(j == 5){   // skip 5
            j++;     // incrementing if j == 5;
            continue;
        }else{
            printf("%d\n", j);
        }
      j++;
    }
return 0;
}

Output of the program:

1
2
3
4
6
7
8
9
10

Process returned 0 (0x0)   execution time : 0.028 s
Press any key to continue.

Do while loop & continue statement

Use of continue statement with do while loop in C is almost the same process as other loops. See the use of this statement with do while loop bellow in the C program.

// use of continue statement with do while loop in C
#include <stdio.h>
int main(){
    int k = 1;

    do{
        if(k == 5){   // skipping 5
            k++;
            continue;
        }else{
            printf("%d\n", k);
            k++;
        }
    }while(k <= 10);

return 0;
}

Output of continue statement program:

Previous pageBreak statement in C

Next pagegoto statement in C