Continue Statement in C

By | February 20, 2023

Prerequisite – Control Statements in C
In C, the “continue” statement is used to skip the current iteration of a loop and move on to the next one. Here are a couple of examples of using “continue” in different loops:

Example-1: Print odd numbers between 1 and 10 using a “for” loop and “continue”

#include <stdio.h>

int main() {
    int i;

    for (i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Output:

1 3 5 7 9

In this example, the “for” loop is used to print the odd numbers between 1 and 10. Within the loop, the if statement is used to check if the value of i is even (i.e., if i divided by 2 has a remainder of 0). If i is even, the “continue” statement is executed, and the loop moves on to the next iteration without executing the printf statement. If i is odd, the printf statement is executed, and the odd number is printed to the console.

Example-2: Print the first 5 multiples of 3 using a “while” loop and “continue”

#include <stdio.h>

int main() {
    int i = 1;
    int count = 0;

    while (count < 5) {
        i++;

        if (i % 3 != 0) {
            continue;
        }

        printf("%d ", i);
        count++;
    }

    return 0;
}

Output:

6 9 12 15 18

In this example, the “while” loop is used to print the first 5 multiples of 3. The loop runs as long as the value of count is less than 5. Within the loop, the variable i is incremented at the beginning of each iteration. The if statement is used to check if i is a multiple of 3 (i.e., if i divided by 3 has a remainder of 0). If i is not a multiple of 3, the “continue” statement is executed, and the loop moves on to the next iteration without executing the printf statement. If i is a multiple of 3, the printf statement is executed, and the multiple of 3 is printed to the console. The variable count is then incremented, and the loop continues until count reaches 5.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.