在C(A #随时回答其他语言)的环,是在休息和继续作为一个均值环结构的书,去下一个迭代?
例子:
1 2 3 4 5 6 7 8
| foreach (DataRow row in myTable.Rows)
{
if (someConditionEvalsToTrue)
{
break; //what's the difference between this and continue ?
//continue;
}
} |
break将完全退出循环,continue将跳过当前迭代。
例如:
1 2 3 4 5 6 7
| for (int i = 0; i < 10; i++) {
if (i == 0) {
break;
}
DoSomeThingWith(i);
} |
中断将导致循环在第一次迭代时退出-将永远不会执行DoSomeThingWith。这里:
1 2 3 4 5 6 7
| for (int i = 0; i < 10; i++) {
if(i == 0) {
continue;
}
DoSomeThingWith(i);
} |
不会对i = 0执行DoSomeThingWith,但循环将继续,对i = 1至i = 9执行DoSomeThingWith。
理解这一点的一个真正简单的方法是在每个关键字后放置单词"loop"。如果这些术语像日常用语一样被阅读,那么它们现在就有意义了。
break环路中断并停止。
continue循环在下一次迭代中继续执行。
中断导致程序计数器跳出最内部循环的范围
1 2 3 4 5
| for(i = 0; i < 10; i++)
{
if(i == 2)
break;
} |
像这样工作
1 2 3 4 5 6
| for(i = 0; i < 10; i++)
{
if(i == 2)
goto BREAK;
}
BREAK:; |
继续跳到循环的末尾。在for循环中,继续跳到增量表达式。
1 2 3 4 5 6 7
| for(i = 0; i < 10; i++)
{
if(i == 2)
continue;
printf("%d", i);
} |
像这样工作
1 2 3 4 5 6 7 8 9
| for(i = 0; i < 10; i++)
{
if(i == 2)
goto CONTINUE;
printf("%d", i);
CONTINUE:;
} |
break将完全停止foreach循环,continue将跳到下一个DataRow循环。
我以前总是搞不清楚我应该使用break还是continue。这有助于我记住:
何时使用中断与继续?
分手-就像分手。很遗憾,你们要分开了。

继续-意味着你要让今天休息一下,明天把它整理好(即跳过当前的迭代)!

有很多人不喜欢break和continue。我看到的关于它们的最新抱怨是javascript:道格拉斯·克罗克福德的好文章。但我发现,有时使用其中一个确实可以简化一些事情,特别是如果您的语言不包括do-while或do-until样式的循环。
我倾向于在搜索某个列表的循环中使用break。一旦发现,继续下去毫无意义,所以你最好辞职。
我使用continue来处理列表中的大多数元素,但仍然希望跳过一些元素。
当对某人或某物的有效响应进行投票时,break语句也很有用。而不是:
1 2 3
| Ask a question
While the answer is invalid:
Ask the question |
您可以消除一些重复并使用:
1 2 3 4
| While True:
Ask a question
If the answer is valid:
break |
我前面提到的do-until循环是针对这个特定问题的更优雅的解决方案:
1 2 3
| Do:
Ask a question
Until the answer is valid |
不需要重复,也不需要break。
所有人都给出了很好的解释。我仍然在发布我的答案,只是为了举例说明是否有帮助。
1 2 3 4 5 6 7 8
| // 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[打印的]&4[打印的]不会显示为有中断。
1 2 3 4 5 6 7 8
| //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]
因此3[打印]将不会显示,因为当i==3时继续
断裂
中断强制循环立即退出。
继续
这与中断相反。它没有终止循环,而是立即再次循环,跳过其余代码。
举例来说
1 2 3 4
| foreach(var i in Enumerable.Range(1,3))
{
Console.WriteLine(i);
} |
打印1、2、3(在单独的行上)。
在i=2时添加中断条件
1 2 3 4 5 6 7
| foreach(var i in Enumerable.Range(1,3))
{
if (i == 2)
break;
Console.WriteLine(i);
} |
现在循环打印1并停止。
用continue替换中断。
1 2 3 4 5 6 7
| foreach(var i in Enumerable.Range(1,3))
{
if (i == 2)
continue;
Console.WriteLine(i);
} |
现在循环打印1和3(跳过2)。
因此,break停止循环,而continue跳过下一个迭代。
请允许我说明显而易见的一点:请注意,无论添加break还是continue,都不会恢复您的程序;也就是说,我陷入了某个错误,然后在记录它之后,我想恢复处理,在下一行之间有更多的代码任务,所以我只是让它通过。
简单回答:
break立即退出循环。继续开始处理下一个项目。(如果有,跳到for/while的评估行)
不幸的是,红宝石有点不同。附言:我的记忆有点模糊,所以如果我错了,请道歉。
它有break/next,而不是break/continue,后者在循环方面的行为相同。
循环(和其他一切一样)是表达式,并且"返回"是它们做的最后一件事。大多数时候,从循环中获取返回值是没有意义的,所以每个人都这样做。
1 2 3 4
| a = 5
while a < 10
a + 1
end |
但是你可以这样做
1 2 3 4
| a = 5
b = while a < 10
a + 1
end # b is now 10 |
然而,许多Ruby代码使用一个块来"模拟"一个循环。典型的例子是
1 2 3
| 10.times do |x|
puts x
end |
由于人们更喜欢用一个块的结果来做事情,所以这就是它变得混乱的地方。break/next表示块上下文中的不同内容。
break将跳出调用块的代码
下一步将跳过块中的其余代码,并将指定的内容"返回"给块的调用方。没有例子就没有任何意义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| 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很棒,但它有一些可怕的角落。这是我多年来所见过的第二个最差的版本——)
为了完全脱离foreach循环,使用break;
要转到循环中的下一个迭代,使用continue;
如果正在循环访问对象集合(如数据表中的行),并且正在搜索特定的匹配项,则break非常有用,当找到该匹配项时,不需要继续搜索其余的行,因此您希望break out。
当您在循环迭代中完成了所需的工作时,continue非常有用。你通常会在一次中情局之后继续。
如果不想使用break,只需增加i的值,使迭代条件为false,循环就不会在下一次迭代中执行。
1 2 3 4
| for(int i = 0; i < list.Count; i++){
if(i == 5)
i = list.Count; //it will make"i<list.Count" false and loop will exit
} |
其他语言:
1 2 3 4 5 6 7 8 9 10
| 'VB
For i=0 To 10
If i=5 then Exit For '= break in C#;
'Do Something for i<5
next
For i=0 To 10
If i=5 then Continue For '= continue in C#
'Do Something for i<>5...
Next |