C++ continue Statement
In C++, the continue
statement is used to skip the current iteration of a loop and move on to the next iteration. When the continue
statement is encountered inside a loop, the current iteration is terminated and program execution resumes at the beginning of the next iteration.
The continue
statement is commonly used in loops to skip over certain elements or iterations when a certain condition is met. Here's an example of using the continue
statement in a for
loop:
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } std::cout << i << std::endl; }
In this example, the for
loop will execute 10 times. When i
is an even number, the continue
statement is executed, which skips the current iteration and moves on to the next iteration. The code after the continue
statement (std::cout << i << std::endl;
) is not executed for even values of i
.
The output of this code will be:
1 3 5 7 9
Note that the continue
statement can only be used inside a loop. If you try to use continue
outside of a loop, the compiler will generate an error.