C continue statement

In the C programming language, the continue statement is used to skip the remaining code inside a loop and move on to the next iteration of the loop. It is typically used in loops such as for, while, or do-while.

The syntax of the continue statement is as follows:

continue;

When the continue statement is encountered inside a loop, the program immediately jumps to the loop’s increment or condition check (depending on the loop type) and starts the next iteration without executing the remaining statements within the loop body.

Here’s an example to illustrate the usage of the continue statement:

#include <stdio.h>

int main() {
    int i;
    
    for (i = 1; i <= 5; i++) {
        if (i == 3) {
            continue; // skip the rest of the code and move to the next iteration
        }
        
        printf("%d\n", i);
    }
    
    return 0;
}

Output:

1
2
4
5

In this example, the for loop iterates from 1 to 5. When i is equal to 3, the continue statement is encountered, and the remaining statements inside the loop (in this case, the printf statement) are skipped for that iteration. The program then moves to the next iteration of the loop.

As a result, the output does not include the number 3, and the loop continues with the numbers 4 and 5.

Continue statement example:

Certainly! Here’s an example that illustrates the usage of the continue statement:

#include <stdio.h>

int main() {
    int i;

    for (i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue; // skip even numbers
        }
        
        printf("%d\n", i);
    }
    
    return 0;
}

Output:

1
3
5
7
9

In this example, we have a for loop that iterates from 1 to 10. Within the loop, the if condition checks if the current value of i is even using the modulo operator %. If i is even, the continue statement is encountered, and the remaining code inside the loop for that iteration is skipped. The program then proceeds to the next iteration.

As a result, the output only includes the odd numbers from 1 to 10 (excluding the even numbers). The continue statement allows you to control the flow of the loop and skip specific iterations based on certain conditions.

C continue statement with inner loop:

Certainly! Here’s an example that demonstrates the usage of the continue statement within an inner loop:

#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 5; i++) {
        printf("Outer loop iteration: %d\n", i);

        for (j = 1; j <= 3; j++) {
            if (j == 2) {
                printf("Inner loop iteration %d skipped\n", j);
                continue; // skip the rest of the inner loop and move to the next iteration
            }
            
            printf("Inner loop iteration: %d\n", j);
        }
    }
    
    return 0;
}

Output:

Outer loop iteration: 1
Inner loop iteration: 1
Inner loop iteration 2 skipped
Inner loop iteration: 3
Outer loop iteration: 2
Inner loop iteration: 1
Inner loop iteration 2 skipped
Inner loop iteration: 3
Outer loop iteration: 3
Inner loop iteration: 1
Inner loop iteration 2 skipped
Inner loop iteration: 3
Outer loop iteration: 4
Inner loop iteration: 1
Inner loop iteration 2 skipped
Inner loop iteration: 3
Outer loop iteration: 5
Inner loop iteration: 1
Inner loop iteration 2 skipped
Inner loop iteration: 3

In this example, we have an outer for loop and an inner for loop. The outer loop iterates from 1 to 5, and the inner loop iterates from 1 to 3. Inside the inner loop, when the value of j is equal to 2, the continue statement is encountered. As a result, the remaining code inside the inner loop for that iteration is skipped, and the program moves to the next iteration of the inner loop.

The output shows the iterations of the outer and inner loops. You can observe that for each iteration of the inner loop when j is equal to 2, the message “Inner loop iteration 2 skipped” is printed, and the rest of the inner loop is bypassed.

This example demonstrates how the continue statement can be used to skip specific iterations within an inner loop while still allowing the outer loop to continue its execution.