C++ Continue Statement
The continue statement in C++ is used to skip the current iteration of a loop and move directly to the next iteration. When the continue statement is executed, the remaining code inside the loop for that iteration is skipped.
The continue statement is commonly used in for, while, and do while loops when certain conditions require skipping part of the loop execution.
Syntax
continue;
Continue Statement in a For Loop
In this example, the continue statement skips the number 3 during the loop execution.
#include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
cout << i << " ";
}
return 0;
}
Output
1 2 4 5
Continue Statement in a While Loop
#include <iostream>
using namespace std;
int main() {
int i = 0;
while(i < 5) {
i++;
if(i == 3) {
continue;
}
cout << i << " ";
}
return 0;
}
Important Notes
- The continue statement skips the remaining code inside the loop for the current iteration.
- The loop does not stop completely; it moves to the next iteration.
- It is useful when certain values or conditions should be ignored during loop execution.
Next Topic
Next, learn about C++ Nested Loops.