In C++, the continue statement is used to skip the current iteration of a loop and move to the next iteration, without executing the remaining code in the current loop iteration. It’s commonly used when you want to skip certain steps under a specific condition while keeping the loop running.
Syntax:
continue;
Note:
1. The continue statement only affects the current iteration of the loop. The loop itself continues running until its exit condition is met.
2. It’s often used when you want to skip certain iterations of the loop but don’t want to break the loop entirely.
Where to use continue statement?
We can use in for, while, and do-while loops.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 4) {
continue; // Skip the rest of the loop when i is 4
}
cout << i << endl;
}
return 0;
}
Output:
1
2
3
5
2
3
5