C++ continue Statement

In this lesson, you will learn about the continue statement in C++, its usage, and an example to better understand the topic.


C++ continue Statement

The “continue” statement in C++ allows you to skip any loop’s current iteration. The program returns to the exact first statement in the loop body when a continue statement is located in the loop, bypassing all subsequent statements for the current iteration. The loop is not ended; instead, the next iteration is started.

Basic syntax

continue;

The continue command skips the current iteration and begins the following when “number” equals 13.

The condition is then reevaluated when the “number” is changed to 12. As a result, the next iterations print the rest of the numbers to 1. Remarkably, decision-making statements are always followed by the “continue” statement.

Example

#include <iostream>

using namespace std;
int main() {
  int number;
  for (number = 20; number >= 1; number--) {
    if (number == 13) {
      continue;
    }
    cout << number << " ";
  }
  return 0;
}

Output

20 19 18 17 16 15 14 12 11 10 9 8 6 5 4 3 2 1

The program skips the iteration when the loop reaches 13, so 13 is not printed.

Note
Please take note that the break statement ends the loop entirely. On the other hand, the continue statement only skips the current iteration.

This concludes the C++ continue Statement lesson. The next lesson will teach about the goto Statement in C++.