How is the “continue” statement used in C++?
In C++, the continue statement is a control flow statement that is used to skip the remaining statements in the current loop body and directly proceed to the next iteration condition check.
The continue statement is commonly used in loop structures such as for and while loops to skip the current iteration under a specific condition and proceed to the next iteration.
When the program reaches a continue statement, it immediately stops the current loop iteration, jumps to the loop condition evaluation, then determines if the condition is met. If it is met, the program proceeds to the next loop iteration; if it is not met, the loop ends.
Here is an example showing the usage of the continue statement.
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue; // 当 i 等于 5 时,跳过当前迭代,进行下一次迭代
}
cout << i << " ";
}
In the above code, when i is equal to 5, the continue statement will skip the cout statement and directly move on to the next loop iteration. Therefore, the output will not include the number 5, but instead, it will print 0 1 2 3 4 6 7 8 9.