Sometimes there is a situation in the program where we want to take control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. The keyword continue allows us to do this. The continue statement skip the present iteration or true expression and jumps to next iteration. Generally continue keyword cannot use with control statement (if, if-else, switch). It is used in loops.
Let’s see an example to demonstrate the use of continue–
1 2 3 4 5 6 7 8 9 10 |
int main() { int i; for( i = 1; i < 6; i++) { if(i == 3) continue; printf("%d\t",i); } } |
Output:
1 |
1 2 4 5 |
Here in above example, the condition i == 3 is satisfied then also it gets skipped. Because of keyword continue.
continue keyword with while
1 2 3 4 5 |
while(1) { continue; printf("saurab"); } |
Output:
1 |
no output |
1 2 3 4 5 |
while(1) { continue; printf("saurab"); } |
Output:
1 |
saurab will print infinite time |