javascript javascript中带标签和不带标签的中断有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5118965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:00:47  来源:igfitidea点击:

what the difference between break with label and without label in javascript

javascriptlabelbreak

提问by dramasea

var num = 0;
for(var i = 0; i < 10; i++){
  for(var j = 0; j < 10 ; j++){
    if(i == 5 && j == 5){
      break;
    }
    num++;
  }
}

console.log(num)

In the above code, I expect the result to be 55 but why the result is 95.

在上面的代码中,我希望结果是 55 但为什么结果是 95。

But why if I added the label, the result become 55?

但是为什么如果我加了标签,结果变成了55呢?

var num = 0;
outermost:
for(var i = 0; i < 10; i++){
  for(var j = 0; j < 10 ; j++){
    if(i == 5 && j == 5){
      break outermost;
    }
    num++;
  }
}

console.log(num);

回答by krtek

when used without label, break only break the current loop, in your case the innermost for. So now j = 6, the condition is now wrong, and the loops continues for 40 more incrementation.

在不带标签的情况下使用时,break 只会中断当前循环,在您的情况下是最里面的 for。所以现在 j = 6,条件现在是错误的,循环继续增加 40。

When you put a label, break go to the "level" of the label, so the two for loops are skipped.

放置标签时,break 会转到标签的“级别”,因此会跳过两个 for 循环。

回答by Matt Ball

Using breakwithout a label breaks the innermost loop which is currently executing.

break不带标签使用会中断当前正在执行的最内层循环。

Using breakwith a label foobreaks the statementlabeled foo.

使用break带有标签foo打破了声明标记foo

MDN breakdocs:

MDNbreak文档

The breakstatement includes an optional label that allows the program to break out of a labeled statement. The breakstatement needs to be nested within this labelled statement. The labelled statement can be any blockstatement; it does not have to be preceded by a loop statement.

break语句包括一个可选标签,允许程序跳出带标签的语句。该break语句需要嵌套在此标记语句中。标记语句可以是任何语句;它不必以循环语句开头。

回答by davin

Without a label, breakwill break out of the inner loop. With a label you can stop execution of nested loops.

没有标签,break会跳出内循环。使用标签,您可以停止执行嵌套循环。

See the examples:

请参阅示例:

https://developer.mozilla.org/en/JavaScript/Reference/Statements/label

https://developer.mozilla.org/en/JavaScript/Reference/Statements/label

回答by Jay Dave

the break is given in only inner for loop. so it breaks only inner for loop when i = j = 5. but the outer loop continues to revolve 10 times. so when i=j=5 the loop will revolve only 5 times and in rest of all cases it will revolve 10 times.

中断仅在内部 for 循环中给出。所以当 i = j = 5 时它只中断内部 for 循环。但外部循环继续旋转 10 次。因此,当 i=j=5 时,循环将只旋转 5 次,而在其他情况下,它将旋转 10 次。

回答by Harish Singh Bisht

when you use break without label , it only breaks the inner loop that is (i=5 j=6) ,(i=5 j=7) ,(i=5 j=8) ,(i=5 j=9) only and loop again starts with (i=6 j=0) to (i=9 j=9) and also count (num ++) startsthats why it show result as 95.

当您使用不带标签的 break 时,它只会中断 (i=5 j=6) ,(i=5 j=7) ,(i=5 j=8) ,(i=5 j=9) 的内部循环only 和 loop 再次从 (i=6 j=0) 开始到 (i=9 j=9) 并且 count (num ++) 开始这就是为什么它显示结果为 95。

bt when you use break with label i.e. break outermost , it breaks out from the loop label as outermost (i.e the outer loop), thats why6 it gives output as 55

bt 当您使用带标签的 break 时,即 break outermost ,它从循环标签中作为最外层(即外循环)中断,这就是为什么 6 它给出的输出为 55

回答by RussellUresti

The first one is only breaking your "j" loop. After it breaks it, it returns to your "i" loop, and increments "i" to 6. Once "i" is 6, it returns to the "j" loop and the if condition is no longer met. So it continues to add up "num".

第一个只是打破你的“j”循环。在它中断之后,它返回到您的“i”循环,并将“i”增加到 6。一旦“i”为 6,它就会返回到“j”循环并且不再满足 if 条件。所以它继续加起来“num”。