Understanding of continue statement in C Language
The continue statement in the C language is utilized to redirect program control to the beginning of a
The continue statement in the C language is utilized to redirect program control to the beginning of a
The “continue” statement in the C programming language is a fundamental control structure that enables precise control over loop execution. It allows programmers to skip a portion of code within a loop when a specific condition is met and proceed to the next iteration. This construct is invaluable for streamlining code, optimizing performance, and handling specific scenarios.
The basic syntax of the “continue” statement is straightforward: you simply write “continue;” within the loop body. When the program encounters this statement, it immediately transfers control back to the beginning of the loop, bypassing any code following the “continue” statement in the current iteration
// Loop statements
continue;
// Some lines of code to be skipped
#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}
Output:
Infinite loop
#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==5){ // If the value of i is equal to 5, it will continue the loop
continue;
}
printf("%d \n", i);
}
// End of for loop
return 0;
}
Output:
1
2
3
4
6
7
8
9
10
As you can observe, the number 5 is not printed on the console because the loop continues when i==5
.
In such cases, the C continue statement exclusively continues the inner loop while leaving the outer loop unaffected.
#include<stdio.h>
int main(){
int i=1, j=1; // Initializing local variables
for(i=1; i<=3; i++){
for(j=1; j<=3; j++){
if(i==2 && j==2){
continue; // It will continue the loop of 'j' only
}
printf("%d %d\n", i, j);
}
}
// End of for loop
return 0;
}
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
As evident, the pair “2 2” is not printed on the console because the inner loop is continued when i==2
and j==2
.
Subscribe to get the latest posts sent to your email.