admin管理员组

文章数量:1331889

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. 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 mand 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. Can someone make it clear with some practical example?

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. 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 mand 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. Can someone make it clear with some practical example?

Share Improve this question edited Sep 5, 2011 at 14:01 David G asked Sep 5, 2011 at 13:45 David GDavid G 96.9k41 gold badges172 silver badges258 bronze badges 3
  • Good point, curious to see a good application of break (and continue), cause most of what i see so far seems to lead to obscure and unreadable code – Luis Commented Sep 5, 2011 at 13:48
  • 2 I've no idea why you use break in this situation – Ruslan Commented Sep 5, 2011 at 13:48
  • You use break when you don't use constants. If you loop an array of objects and check one value in each object to be 3, you don't really know it will be when i == 3, right? However, in your example, the usage of break is stupid, yes. – Emil Commented Sep 5, 2011 at 13:57
Add a ment  | 

6 Answers 6

Reset to default 6

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.

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

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. This can quickly lead to unreadable code.

break and continue are more readable

For example when you're searching something in an array of 10 items, you want to loop from 0 .. 9. However, as soon as a result is found, there's no use for searching further, so you can break and jump out of the loop.

In your example, its a bad loop condition.

However it would get really plicated 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. In reality you wouldn't write anything like that.

However, break has it's advantages. 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.

本文标签: Why do we use breaks in JavaScript loopsStack Overflow