In this C++ tutorial, you will learn about continue statement, and how to use this continue statement in loops to control their execution flow, with the help of example programs.

C++ Continue Statement

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

We can use continue statement in While Loop, Do-while Loop and a For Loop.

Syntax of Continue

The syntax of continue statement is

</>
Copy
continue;

Please note that continue statement can be used only inside a loop statement.

Continue statement in While loop

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.

Refer C++ While Loop tutorial.

main.cpp

</>
Copy
#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.

Continue statement in 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.

Refer C++ Do-While Loop tutorial.

main.cpp

</>
Copy
#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

Continue statement 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.

Refer C++ For Loop tutorial.

main.cpp

</>
Copy
#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.

main.cpp

</>
Copy
#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

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

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

mian.cpp

</>
Copy
#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.