How to Use the continue Statement in C/C++

Filed Under: C Programming
Continue Statement C

In this article, we’ll take a look at using the continue statement in C/C++.

This is one of the easiest and the most basic keywords in C/C++, which gives programmers control over loops.

Let’s take a quick look, using some examples!


Using the continue statement in C/C++

The continue statement is exactly as the name suggests. Since we use this in loops, it will skip over the remaining body of the current loop, and continue to the next iteration.

We can use this inside any loops like for, while, or do-while loops.

Let’s start with while loops now.


Using the continue statement with a while loop

We’ll take a simple example to illustrate this concept.

while (condition) {
    if (something)
        continue;
    else
        printf("Hi\n");
}

In this case, if something was True, we will continue onto the next iteration. So, in this case, Hi will not be printed, as the loop directly moves to the while(condition) check.

Take the below program, in C:

#include <stdio.h>

int main() {
    int i = 1;
    while (i <= 5) {
        if (i == 3) {
            i++;
            continue;
        }
        printf("i = %d\n", i);
        i++;
    }
    return 0;
}

Output

1
2
4
5

In this case, we start from i=1 and keep iterating through the while loop. If i = 3, we increment i and continue to the next iteration.

So, when i=3, it will not be printed, since we skip the body and proceed to the next iteration!

Note that we carefully placed an i++ increment before the continue, as otherwise, we will end up in an infinite loop, as the value of i won’t change when using continue normally!


Using continue along with a for loop

We can write the same program, using a for loop too!

#include <stdio.h>

int main() {
    for (int i=1; i<=5; i++) {
        if (i == 3) {
            i++;
            continue;
        }
        printf("i = %d\n", i);
    }
    return 0;
}

We get the same output as before.


Using continue with a do-while loop

We can also use this on a do-while loop, since this is also an iterative loop.

#include <stdio.h>

int main() {
    int i=10; // Set i = 10
    do {
        if (i > 5) { // Initially, we will go to this statement, as it is a do-while loop!
            i = 0;
            continue;
        }
        printf("i = %d\n", i);
    } while (i <= 5)
    return 0;
}

In this case, since we have a do-while loop, we go to the body of the loop before we do our condition check. So, when i=10, we reassign it to 0 and continue. So, the output will simply be integers from 0 to 5!

Output

0
1
2
3
4
5


Conclusion

In this article, we learned how we could use the continue statement in C/C++ to have control over loop statements!

If you want similar articles on C, do go through our C programming tutorials!


References


Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages