AlphaCodingSkills

C - Continue Statement


Advertisements

Previous Page Next Page

The continue statement in C let the program skip a block of codes for current iteration in a loop. Whenever continue statement condition is fulfilled, it brings the program to the start of loop. If the continue statement is used in a nested loop (loop inside loop), it will skip innermost loop's code block, whenever condition is fulfilled.

Continue statement with While loop

#include <stdio.h>
 
int main (){
    int i = 6;
    int j = 0;
    while (j < i){
        j++;
        if(j == 4){
           printf("this iteration is skipped.\n");
           continue;
        }
        printf("%i\n", j);
    }
    return 0;
}

Output

1
2
3
this iteration is skipped.
5
6

Continue statement with For loop:

#include <stdio.h>
 
int main (){
    for (int i = 1; i <= 6; i++){
        if(i == 4 ){
           printf("this iteration is skipped.\n"); 
           continue;
        }
        printf("%i\n", i);
    }
    return 0;
}

Output

1
2
3
this iteration is skipped.
5
6

Continue statement with Nested loop:

The Continue statement skip the inner loop's block of codes whenever condition is fulfilled. In below mentioned example, program skips the inner loop only when j = 100.

//Nested loop without continue statement
#include <stdio.h>
 
int main (){
    for (int i = 1; i <= 3; i++){
      for (int j = 10; j <= 1000; j = j * 10){
        printf("%i\n",i*j);
      }
    }
    return 0;
}

//Nested loop with continue statement
#include <stdio.h>
 
int main (){
    for (int i = 1; i <= 3; i++){
      for (int j = 10; j <= 1000; j = j * 10){
        if(j == 100 ){
           continue;
        }
        printf("%i\n",i*j);
      }
    }
    return 0;
}

Output

# output of nested loop without continue statement
10
100
1000
20
200
2000
30
300
3000

# output of nested loop with continue statement
10
1000
20
2000
30
3000

Previous Page Next Page