跳过当前' for '迭代的其余部分,开始一个新的迭代

C++ Skipping the rest of the current `for` iteration and beginning a new one.

本文关键字:迭代 开始 一个 for 余部      更新时间:2023-10-16

考虑下面的c++代码

for(int i=0; i<=N; ++i)
{
if(/* Some condition 1 */) {/*Blah Blah*/}
if(/* Some condition 2  */){/*Yadda Yadda*/}
}

是否存在关键字/命令,以便如果条件1的计算结果为true并且执行/*Blah Blah*/,我可以跳过当前迭代的其余部分,并通过增加i开始新的迭代。

我知道最接近这种语句跳过的是break,但它完全终止了循环。

我想可以通过使用一些标志和if语句来做到这一点,但是一个简单的关键字将非常有帮助。

使用关键字continue,它将'继续'到下一次循环。

这种情况似乎更适合if. else..而不是continue,尽管continue也可以。

for(int i=0; i<=N; ++i)
{
    if(/* Some condition 1 */)
    {/*Blah Blah*/}
    else if(/* Some condition 2  */)
    {/*Yadda Yadda*/}
}

使用Continue语句,停止当前循环并继续下一个循环,而不是完全中断

继续