C++ Continue Statement

Continue statement skips the execution of further statements in the block and continues with the next iteration.

You can apply continue statement to while loop, do-while loop and a for loop.

In this tutorial, we shall go through examples illustrating C++ looping statements with continue statements in them.

Syntax of Continue

Following is the syntax of continue statement.

continue;

Please note that you can use continue statement only within a loop statement.

C++ Continue in While Loop

Continue statement can be used to skip the execution of statements in the C++ While Loop after the continue statement.

In the following example, while loop tries to print numbers from 0 to 9. But during fourth iteration when i becomes 4, continue statement skips the execution of printing the number.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int i=0;
   while (i < 10) {
      if (i==4) {
         i++;
         continue;
      }
      cout << i << "  ";
      i++;
   }
}

Output

0  1  2  3  5  6  7  8  9

Also, the control variable i is not incremented because of the continue statement. So, we incremented i before executing the continue statement. If i not modified here, the while loop may become indefinite loop.

C++ Continue in Do-while Loop

Continue statement can be used with a C++ Do-While Loop.

In the following example, do-while loop tries to print numbers from 0 to 9. But during fifth iteration when i becomes 5, continue statement skips the execution of further statements in the loop.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int i=0;
   do {
      if (i==5) {
         continue;
      }
      cout << i << "  ";
   } while (++i < 10);
}

Output

0  1  2  3  4  6  7  8  9

C++ Continue in For Loop

In the following example, we have written a for loop to print numbers from 0 to 9. Also, we have conditionally employed a continue statement to execute when the number reaches 7. When continue statement executes, the program control skips the execution of next statements in the loop, and continues with the next iteration.

C++ Program

#include <iostream>
using namespace std;

int main() {
   for (int i=0; i < 10; i++) {
      if (i==7) {
         continue;
      }
      cout << i << "  ";
   }
}

Output

0  1  2  3  4  5  6  8  9

Continue Statement in Nested Loop

Continue statement affects only its surrounding loop. So, if you are using a continue statement in a nested loop, please note that it continues with next iteration only in its surrounding loop.

In the following example, we shall write a continue statement inside nested while loop. The program prints a pattern.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int i = 1;
   while (i <= 5) {
      int j = 1;
      while (j <= 5) {
         if(j>i) {
            j++;
            continue;
         }
         cout << " *";
         j++;
      }
      cout << "\n";
      i++;
   }
}

Output

*
 * *
 * * *
 * * * *
 * * * * *

Continue Statement in If Else Block

We know that continue statement could be written only inside a looping statements.

In the following example, we shall write a continue statement inside C++ If Else statement, with no loop surrounding the continue statement.

C++ Program

#include <iostream>
using namespace std;

int main() {
   int n = 20;
   if (n%2 == 0) {
      if(n%10 == 0) {
         continue;
      }
      cout << "something even" << endl;
   } else {
      cout << "odd" << endl;
   }
}

Output

d:\workspace\cpp\main.cpp: In function 'int main()':
d:\workspace\cpp\main.cpp:8:10: error: continue statement not within a loop
    8 |          continue;
      |          ^~~~~~~~
The terminal process terminated with exit code: 1

We got a compilation error stating that continue statement is not within loop.

Conclusion

In this C++ Tutorial, we learned about continue statement, what it does to surrounding loop statements, with example C++ programs.