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

tps://piembsystech.com/c-loops/">loop. This statement allows you to skip certain lines of code within the loop and proceed with the next iteration. Its primary purpose is to handle specific conditions, enabling you to bypass certain code for particular situations.

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

Syntax:

// Loop statements
continue;
// Some lines of code to be skipped

Continue Statement Example 1:

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

Output:
Infinite loop

Continue Statement Example 2:

#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.

C Continue Statement with Inner Loop:

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.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading