C continue Statement

Introduction

In the previous chapters, we explored various control flow statements in C, including loops and the break statement. In this chapter, we will focus on the continue statement. The continue statement is used to skip the current iteration of a loop and immediately proceed to the next iteration. It is particularly useful when you want to skip certain elements or conditions within a loop.

What is a continue Statement?

The continue statement is used within loops (for, while, and do-while) to skip the rest of the code inside the loop for the current iteration and jump to the next iteration. When the continue statement is encountered, the loop’s control expression (condition) is re-evaluated, and the next iteration begins.

Syntax

The basic syntax of a continue statement in C is as follows:

continue;

Usage in Loops

The continue statement can be used in all types of loops (for, while, and do-while) to skip certain conditions within the loop body and proceed with the next iteration.

Example: Using continue in a for Loop

Let’s look at an example to understand how the continue statement works in a for loop.

Example: Skipping Even Numbers

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue; // Skip the iteration when i is even
        }
        printf("%d\n", i); // Printing the value of i
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

1
3
5
7
9

In this example, the loop skips the printing of even numbers and only prints the odd numbers between 1 and 10.

Example: Using continue in a while Loop

Example: Skipping a Specific Condition

#include <stdio.h>

int main() {
    int i = 1;

    while (i <= 10) {
        if (i == 5) {
            i++; // Update the loop counter to avoid an infinite loop
            continue; // Skip the iteration when i is 5
        }
        printf("%d\n", i);
        i++;
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

1
2
3
4
6
7
8
9
10

Example: Using continue in a do-while Loop

Example: Skipping a Specific Condition

#include <stdio.h>

int main() {
    int i = 1;

    do {
        if (i == 3) {
            i++; // Update the loop counter to avoid an infinite loop
            continue; // Skip the iteration when i is 3
        }
        printf("%d\n", i);
        i++;
    } while (i <= 5);

    return 0; // Returning 0 to indicate successful execution
}

Output:

1
2
4
5

Nested Loops and continue

When using nested loops, the continue statement only affects the innermost loop in which it is placed.

Example: Using continue in Nested Loops

Example: Skipping a Specific Condition in the Inner Loop

#include <stdio.h>

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (j == 2) {
                continue; // Skip the iteration when j is 2
            }
            printf("i = %d, j = %d\n", i, j);
        }
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3

In this example, the inner loop skips the printing when j is equal to 2, but the outer loop continues to execute.

Simple C Programs to Demonstrate continue Statement

Program 1: Printing Odd Numbers

Example:

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue; // Skip even numbers
        }
        printf("%d\n", i); // Printing odd numbers
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

1
3
5
7
9

Program 2: Skipping Multiples of a Specific Number

Example:

#include <stdio.h>

int main() {
    for (int i = 1; i <= 20; i++) {
        if (i % 3 == 0) {
            continue; // Skip multiples of 3
        }
        printf("%d\n", i);
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

1
2
4
5
7
8
10
11
13
14
16
17
19
20

Program 3: Skipping Elements in an Array

Example:

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < n; i++) {
        if (arr[i] == 30) {
            continue; // Skip the element 30
        }
        printf("Element %d: %d\n", i, arr[i]);
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Element 0: 10
Element 1: 20
Element 3: 40
Element 4: 50

Conclusion

The continue statement is a powerful control flow tool in C that allows you to skip the current iteration of a loop and proceed to the next iteration. It provides greater control over the execution flow of your loops, enabling you to handle specific conditions more efficiently. By understanding and using the continue statement effectively, you can write more robust and flexible C programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top