javascript中带标签的break和不带标签的break有什么区别
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)
在上面的代码中,我期望结果是55,但为什么结果是95。
但是为什么如果我添加了标签,结果就变成了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);
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.
But why if I added the label, the result become 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当不带标签使用时,break只会中断当前循环,在您的情况下是最里面的for。所以现在 j = 6,条件现在是错误的,并且循环继续进行 40 个增量。
当你放置一个标签时,break转到标签的“级别”,因此两个for循环被跳过。
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.
When you put a label, break go to the "level" of the label, so the two for loops are skipped.
使用不带标签的
break
会中断当前正在执行的最内层循环。将
break
与标签foo
一起使用会破坏标记为foo
的语句。MDN
break
文档:Using
break
without a label breaks the innermost loop which is currently executing.Using
break
with a labelfoo
breaks the statement labeledfoo
.MDN
break
docs:如果没有标签,
break
将跳出内部循环。使用标签可以停止嵌套循环的执行。请参阅示例:
https://developer.mozilla.org/en/JavaScript/Reference /语句/标签
Without a label,
break
will break out of the inner loop. With a label you can stop execution of nested loops.See the examples:
https://developer.mozilla.org/en/JavaScript/Reference/Statements/label
第一个只是打破你的“j”循环。中断后,它返回到“i”循环,并将“i”递增到 6。一旦“i”为 6,它返回到“j”循环,并且不再满足 if 条件。所以它继续加“num”。
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".
仅在内部 for 循环中给出中断。
因此,当 i = j = 5 时,它仅中断内部 for 循环。
但外循环继续旋转10次。
因此,当 i=j=5 时,循环将仅旋转 5 次,而在其余所有情况下,它将旋转 10 次。
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.
当您使用不带 label 的 break 时,它只会中断内部循环 (i=5 j=6) ,(i=5 j=7) ,(i=5 j=8) ,(i=5 j=9)仅并再次循环从 (i=6 j=0) 到 (i=9 j=9) 开始,并且还计数 (num ++) 开始,这就是为什么它显示结果为 95。bt
当您使用带有标签的中断时,即中断最外层,它从循环标签中突破为最外层(即外循环),这就是为什么6它给出的输出为 55
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.
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