C#循环-中断与继续

时间:2020-03-05 18:38:41  来源:igfitidea点击:

在C(其他语言随时可以回答)循环中,break和continue作为离开循环结构并转到下一个迭代的方法有什么区别?

例子:

foreach (DataRow row in myTable.Rows)
{
    if (someConditionEvalsToTrue)
    {
        break; //what's the difference between this and continue ?
        //continue;
    }
}

解决方案

回答

" break"将完全停止" foreach"循环," continue"将跳至下一个" DataRow"。

回答

" break"将完全退出循环," continue"将仅跳过当前迭代。

例如:

for (int i = 0; i < 10; i++) {
    if (i == 0) {
        break;
    }

    DoSomeThingWith(i);
}

中断将导致循环在第一次迭代中退出," DoSomeThingWith"将永远不会执行。这里:

for (int i = 0; i < 10; i++) {
    if(i == 0) {
        continue;
    }

    DoSomeThingWith(i);
}

不会为" i = 0"执行" DoSomeThingWith",但是循环将继续,并且将为" i = 1"至" i = 9"执行" DoSomeThingWith"。

回答

理解这一点的一种非常简单的方法是在每个关键字之后放置" loop"一词。如果这些术语像日常用语一样被阅读,则现在变得有意义。

break循环循环中断并停止。

continue循环循环在下一次迭代时继续执行。

回答

有很多人不喜欢breakcontinue。我最近看到的有关它们的投诉是Douglas Crockford撰写的JavaScript:The Good Parts。但是我发现有时使用其中之一确实可以简化事情,特别是如果语言不包含" do-while"或者" do-until"样式的循环。

我倾向于在循环中使用" break"来搜索列表。一旦找到,就没有继续的意义,因此我们最好退出。

在对列表中的大多数元素进行处理时,我会使用" continue",但仍然希望跳过其中的一些内容。

当查询某人或者某物的有效响应时,break语句也很方便。代替:

Ask a question
While the answer is invalid:
    Ask the question

我们可以消除一些重复和使用:

While True:
    Ask a question
    If the answer is valid:
        break

我之前提到的do-until循环是针对特定问题的更优雅的解决方案:

Do:
    Ask a question
    Until the answer is valid

无需重复,也不需要" break"。

回答

不幸的是,Ruby有点不同。
PS:对此我的记忆有些模糊,如果我错了,我深表歉意

它具有break / next而不是break / continue,它们在循环方面的表现相同

循环(像其他所有循环一样)是表达式,并"返回"它们所做的最后一件事。大多数时候,从循环中获取返回值是没有意义的,因此每个人都这样做

a = 5
while a < 10
    a + 1
end

但是,我们可以这样做

a = 5
b = while a < 10
    a + 1
end # b is now 10

但是,许多红宝石代码都使用块来"模拟"循环。
典型的例子是

10.times do |x|
    puts x
end

人们通常希望以块的结果来做事,这就是混乱的地方。
在块的上下文中,break / next意味着不同的事物。

中断将跳出调用该块的代码

接下来将跳过该块中的其余代码,并将我们指定的内容"返回"给该块的调用者。没有示例,这毫无意义。

def timesten
    10.times{ |t| puts yield t }
end

timesten do |x|
   x * 2
end
# will print
2
4
6
8 ... and so on

timesten do |x|
    break
    x * 2
end
# won't print anything. The break jumps out of the timesten function entirely, and the call to `puts` inside it gets skipped

timesten do |x|
    break 5
    x * 2
end
# This is the same as above. it's "returning" 5, but nobody is catching it. If you did a = timesten... then a would get assigned to 5

timesten do |x|
    next 5
    x * 2
end 
# this would print
5
5
5 ... and so on, because 'next 5' skips the 'x * 2' and 'returns' 5.

嗯是的。 Ruby很棒,但是它有一些糟糕的情况。这是我多年来使用它的第二个最糟糕的情况:-)

回答

中断会导致程序计数器跳出最内层循环的范围

for(i = 0; i < 10; i++)
{
    if(i == 2)
        break;
}

像这样工作

for(i = 0; i < 10; i++)
{
    if(i == 2)
        goto BREAK;
}
BREAK:;

继续跳转到循环的结尾。在for循环中,继续跳转到增量表达式。

for(i = 0; i < 10; i++)
{
    if(i == 2)
        continue;

    printf("%d", i);
}

像这样工作

for(i = 0; i < 10; i++)
{
    if(i == 2)
        goto CONTINUE;

    printf("%d", i);

    CONTINUE:;
}

回答

简单的答案:

中断立即退出循环。
继续开始处理下一个项目。 (如果有,请跳转至for / while的评估行)

回答

请让我指出显而易见的一点:请注意,既不添加break也不继续,将恢复程序;即我陷入了某个错误,然后在记录它之后,我想恢复处理,并且在下一行之间有更多的代码任务,所以我让它彻底解决了。

回答

所有人都给出了很好的解释。我仍在发布我的答案,仅举一个例子,如果有帮助的话。

// break statement
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        break; // It will force to come out from the loop
    }

    lblDisplay.Text = lblDisplay.Text + i + "[Printed] ";
}

这是输出:

0[Printed] 1[Printed] 2[Printed]

因此,当i == 3时会中断,因此不会显示3 [Printed]和4 [Printed]

//continue statement
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        continue; // It will take the control to start point of loop
    }

    lblDisplay.Text = lblDisplay.Text + i + "[Printed] ";
}

这是输出:

0[Printed] 1[Printed] 2[Printed] 4[Printed]

所以当i == 3时,因为继续显示,所以不会显示3 [Printed]