What is the purpose of “continue” in C++?
在C++中,continue语句被用于在循环体中跳过当前迭代,并开始下一次迭代。当遇到continue语句时,程序将会立即跳转到循环开始的地方,继续执行下一次迭代。
The continue statement is often used with loop statements, such as for loops and while loops. Its purpose is to skip certain specific conditions and only execute the code after the continue statement in the loop body. This can be used to avoid executing specific code blocks under certain conditions or to skip loops with a large number of iterations.
Here is an example of using the continue statement:
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
cout << i << " ";
}
In the mentioned code, when i equals 5, the continue statement will skip the current iteration and go directly to the next one. Therefore, the output will be: 0 1 2 3 4 6 7 8 9. As a result, number 5 is skipped.