在C#(随时回答其他语言)循环,有什么区别之间破裂和继续作为一种手段离开该结构的循环,走向下一次?

例如:

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

解决方案

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);
}

不执行 DoSomeThingWith 对于 i = 0, 但循环 继续DoSomeThingWith 将执行的对 i = 1i = 9.

其他提示

一个很简单的方法来理解这是把单词"循环"之后的每个关键词。该条款现在的意义,如果他们只是读喜欢的日常短语。

break 循环循环被破坏而停止。

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

打破 导致程序的反跳出范围的最内在的循环

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

这样工作

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

继续 跳到结束循环。在一个循环,继续跳到的增量的表达。

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:;
}

break 会停止的 foreach 回路完全, continue 会跳到下一个 DataRow.

我总是感到困惑我是否应该使用突破,或继续。这是什么帮我记住:

当使用破vs继续吗?

  1. 打破 -这就像打破。这是可悲的,你们是离别。

Break

  1. 继续 -意味着你要给今天的休息和进行排序它所有的明天(即跳过前迭代)!

Continue

有超过一些人不喜欢 breakcontinue.最新的控诉,我看到他们是在 JavaScript:好的部分 道格拉斯*Crockford.但我发现,有时使用的一个他们真的很简化了的事情,尤其是如果你的语言并不包括 do-whiledo-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 需要。

所有的都给予了非常良好的说明。我仍然张贴我的回答只是举个例子如果可以提供帮助。

// 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[印]1[印]2[印]

所以3[印]和4[印]将不被显示为有休息的时候我==3

//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[印]1[印]2[印]4[印]

所以3[印]将不会显示存在继续时,我==3

打破

打破力循环,以立即退出。

继续

这并相对的突破。而不是终止循环,它立即再循环,跳过其他的代码。

通过实例

foreach(var i in Enumerable.Range(1,3))
{
    Console.WriteLine(i);
}

印第1、2、3(在单独的条线)。

增加一个休息的条件i=2

foreach(var i in Enumerable.Range(1,3))
{
    if (i == 2)
        break;

    Console.WriteLine(i);
}

现在的环印刷品1并停止。

替代休息继续。

foreach(var i in Enumerable.Range(1,3))
{
    if (i == 2)
        continue;

    Console.WriteLine(i);
}

现在循环,印1和3(跳2).

因此, break 停的循环,而 continue 跳到下一个迭代。

红宝石不幸的是,是有点不同。PS:我的记忆正在一点点模糊的,在这样的道歉如果我错了

而不是中断/时继续,这已经打下,其表现相同条款的循环

循环(其他事物一样)的表达形式,而"回归"的最后的事情,他们做到了。大部分时间,得到的回报价值从一个循环是毫无意义的,所以每个人都只做这个

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

因为它是从更加普遍为人们想要做的事情的结果的一个方框,这是它变得混乱。休息下的意思是不同的事情在上下文中的一块。

休息会跳出来代码,称为块

下一步将跳过其他的代码中的方框,而"回归"什么你指定的呼叫者的区块。这没有任何意义,没有例子。

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.

所以是的.红宝石是真棒,但是它有一些可怕的角落的情况。这是第二个最糟糕的我已经看到在我多年的使用:-)

简单的回答:

打破 退出循环。
继续 开始处理下一个项目。(如果有任,通过跳到评估行为/话)

请让我国显而易见的:请注意,添加既没有突破也没有继续下去,将会恢复你的节目;即我被困于某些差错,则登录后,我想恢复处理,并有更多的代码任务之间的下一步行,所以我只是让它掉下通过。

打破完全出的foreach循环, 打破 是用;

去到下一代的循环, 继续 是用;

打破 是有用的,如果你是循环的集合的目的(如排在一个数据表)以及你正在寻找一个特别的比赛,当你发现匹配,也没有必要继续通过其余行,使你想要打破出。

继续 是有用的,当你完成了什么你需要在边一个回路迭代。你通常会有的 继续 后一个 如果.

如果你不想使用 打破 你只是增加价值的,我在这样一种方式,它使迭代病假和循环将不执行上的下一次迭代。

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
}

至于其他语文:

'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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top