简体   繁体   English

为什么我们在JavaScript循环中使用中断?

[英]Why do we use breaks in JavaScript loops?

I know "break" is used to stop a loop before the satisfaction of a particular condition but I am really confused WHY use break when we have the option to make our criteria better. 我知道“break”用于在满足特定条件之前停止循环但我真的很困惑为什么在我们可以选择使我们的标准更好时使用break。 For example look at this piece of code: 例如,看看这段代码:

var i = 0;
for (i = 0; i <= 10; i++) {
    if (i == 3) {
        break;
    }
    document.write("The number is " + i);
    document.write("<br />");
}

I know it very well that this break command will terminate the loop as the value of the variable reaches to 3 but my question is why we use break instead of making our condition like i<=3. 我非常清楚这个break命令将终止循环,因为变量的值达到3但我的问题是为什么我们使用break而不是像i <= 3那样使我们的条件。 Can someone make it clear with some practical example? 有人可以用一些实际的例子说清楚吗?

A simple example would be where you are looping through objects in an array, looking for one that satisfies a certain condition. 一个简单的例子是循环遍历数组中的对象,寻找满足某个条件的对象。 When you've found that object, you don't need to check the rest so use break . 当您找到该对象时,您无需检查其余对象,因此请使用break

for (var i = 0; i < arr.length; i++) {
  var e = arr[i];
  if (someCondition(e)) {
    break;
  }
}
// use e

vs VS

for (var i = 0; !someCondition(arr[i]); i++);
var e = arr[i];
// use e

Yes you can have a better condition but that means your putting logic in the for loop statement rather then the body. 是的,你可以有一个更好的条件,但这意味着你的推送逻辑在for循环语句而不是正文。 This can quickly lead to unreadable code. 这可能很快导致无法读取的代码。

break and continue are more readable breakcontinue 更具可读性

例如,当您在10个项目的数组中搜索某些内容时,您希望从0开始循环.9。但是,一旦找到结果,就无法进一步搜索,因此您可以break并跳出循环。

In your example, its a bad loop condition. 在您的示例中,它是一个糟糕的循环条件。

However it would get really complicated when dealing with other objects. 但是在处理其他对象时会变得非常复杂。 See my example. 看看我的例子。

var i = 0;
var myArrayOfObjects = ....
for (i = 0; i <= 10; i++) {
    if (myArrayOfObject[i].someProperty == 3) {
        break;
    }
    document.write("The number is " + i);
    document.write("<br />");
}

I'm pretty sure this is just a piece of code to show you how break works. 我很确定这只是一段代码,向您展示如何break工作。 In reality you wouldn't write anything like that. 实际上你不会写那样的东西。

However, break has it's advantages. 但是, break有它的优点。 You can exit the loop in any moment you want (not only when loop condition says so) 您可以随时退出循环(不仅在循环条件如此说明时)

To add to what other people have said, sometimes you need to break only when a certain event is fired. 要添加到其他人所说的内容中,有时您只需要在某个事件被触发时中断。 Code similar to this: 代码与此类似:

while(true)
{
//run application
if(event)

   break;


}
//end application

makes sense in the context of many applications, games especially. 在许多应用程序,特别是游戏的背景下有意义。

Also, sometimes you may need to check for a condition in the middle of the loop and not in the start of it, so using break does not just make code more readable, it's essential in some cases. 此外,有时您可能需要检查循环中间的条件而不是开始时的条件,因此使用break不仅会使代码更具可读性,而且在某些情况下这是必不可少的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM