Table of Contents
The continue statement skips the remaining statements in the current iteration of the nearest enclosing loop and proceeds with the next iteration. Unlike break, it does not terminate the loop.
The behavior of continue depends on the type of loop in which it is used. In a for loop, control proceeds to the loop’s iteration expression 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 << '\n';
}
return 0;
}
Output #
1
2
4
5
Explanation #
- The
continuestatement skips the remaining statements in the current loop iteration. - The
forloop generates values from1through5. - When
numberbecomes3, the conditionnumber == 3evaluates totrue. continueis executed, so thestd::coutstatement is skipped for that iteration.- Execution then proceeds with the loop’s iteration expression,
++number. - The loop continues normally with the next value,
4. - Unlike
break,continuedoes not terminate the loop. return 0;terminates the program successfully.