Table of Contents
The continue statement skips the remaining statements in the current iteration of the nearest enclosing loop and begins the next iteration. Unlike break, it does not terminate the loop.
The behavior after continue depends on the type of loop. In a for loop, the iteration expression is executed before the next condition check.
Source Code #
#include <iostream>
int main()
{
for (int number = 1; number <= 5; ++number)
{
if (number == 3)
{
continue;
}
std::cout << "Number: " << number << '\n';
}
return 0;
}
Output #
Number: 1
Number: 2
Number: 4
Number: 5
Explanation #
- The
forloop iterates through the values from1to5. - The
ifstatement checks whethernumberis equal to3. - When
numberis3, thecontinuestatement is executed. continueskips the remaining statements in the current iteration, so3is not printed.- For a
forloop, execution proceeds to the iteration expression++numberaftercontinue. - The loop then evaluates its condition and continues with the next iteration.
- Unlike
break,continuedoes not terminate the loop. return 0;terminates the program successfully.