views:

1248

answers:

9

I tried this:

for(i=0;i<5;i++)
{
    for(j=i+1;j<5;j++)
    {
        break(2);
    }
    alert(1)
};

only to get:

SyntaxError: missing ; before statement

+2  A: 

break doesn't take parameters. There are two workarounds:

  1. Wrap them in a function and call return

  2. Set a flag in the inner loop and break again right after the loop if the flag is set.

Aaron Digulla
Actually, it does take parameters. :) https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/break
deceze
Does that work in IE?
Aaron Digulla
Aaron: I just tested in IE6; it does :)
Noon Silk
+24  A: 

You should be able to break to a label, like so:

function foo ()
{
    dance:
    for(var k = 0; k < 4; k++){
        for(var m = 0; m < 4; m++){
            if(m == 2){
                break dance;
            }
        }
    }
}
Noon Silk
That's Java, not JavaScript.
Aaron Digulla
Breakdance. Ha ha , good one.
paxdiablo
@Aaron, JavaScript has labels also.
Brian Gianforcaro
Actually, it's JavaScript too.
Sohnee
Aaron: I've tested it in FF3, I believe you are wrong.
Noon Silk
No, that works in JavaScript too
Justin Johnson
It's still ugly. ;)
Glenn
How is it ugly at all? it's powerful and elegant.
Brian Gianforcaro
I read it as "dance: ...goto dance". I've never found myself needing this convention. "return" from a function is the best approach, IMHO.
Glenn
paxdiablo: Hah, your name change had me a bit confused :)
Noon Silk
Okay. I tried to undo the -1 but SO won't let me :(
Aaron Digulla
@Aaron, edit the answer (trivially, e.g., put a space between a ')' and '{' characters) then reverse your downvote quick as you can. You can't reverse downvotes over a certain age *unless the answer is edited*.
paxdiablo
@Glenn but it still works
retornam
+1, just for break dance
Nader Shirazie
+2  A: 

Unfortunately you'll have to set a flag or use labels (think old school goto statements)

var breakout = false;

for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
    breakout = true;
    break;
}
if (breakout) break;
alert(1)
};

The label approach looks like

end_loops:
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
    break end_loops;
}
alert(1)
};

edit: label incorrectly placed. also see http://www.devguru.com/Technologies/ecmascript/quickref/break.html and http://www.daaq.net/old/javascript/index.php?page=js+exiting+loops&amp;parent=js+statements

Jonathan Fingland
A: 

Break 1st loop:

for(i=0;i<5;i++)
{
  for(j=i+1;j<5;j++)
  {
    //do something

    break;
  }
  alert(1);
};

Break both loops:

for(i=0;i<5;i++)
{
  var breakagain = false;
  for(j=i+1;j<5;j++)
  {
    //do something

    breakagain = true;
    break;
  }
  alert(1);
  if(breakagain)
    break;
};
o.k.w
+5  A: 

You need to name your outer loop and break that loop, rather than your inner loop - like this.

outer_loop: 
for(i=0;i<5;i++) {
    for(j=i+1;j<5;j++) {
     break outer_loop;
    }
    alert(1);
}
Sohnee
Wow, I didn't know we can name loops, great info! +1
o.k.w
@o.k.w: you don't name the loop. You just label a position in the source.
xtofl
Never knew JavaScript supported labels. I wonder why I've never seen that before. :O
Nathan Taylor
@xtofl: Not exactly. You associate a label with a statement (which could be a loop) by placing it immediately before. It's only useful with a block statement, since the label is only available with the break and continue statements within the block.
Tim Down
@Nathan: probably because it's very poor practice: if you need to use labels in your code, it's a sign that you need to rewrite your code ;-)
NickFitz
@NickFitz: you could make a performance argument for it. In the case of nested loops labels could be useful to break out of an outer loop. While it may more elegant and modular to avoid nested loops by moving inner loops to separate functions, it will run marginally slower because of the extra function calls.
Tim Down
+1  A: 

See Aaron's. Otherwise: j=5;i=5 instead of break.

Glenn
+2  A: 
loop1:
    for (var i in set1) {
loop2:
        for (var j in set2) {
loop3:
            for (var k in set3) {
                break loop2;  // breaks out of loop3 and loop2
            }
        }
    }

code copied from http://stackoverflow.com/questions/183161/best-way-to-break-from-nested-loops-in-javascript/183197#183197

Please search before posting a question. The link was the FIRST related question I saw on the left side of this page!

Jim
A: 
function myFunction(){
  for(var i = 0;i < n;i++){
    for(var m = 0;m < n;m++){
      if(/*break condition*/){
        goto out;
      }
    }
  }
out:
 //your out of the loop;
}
junmats
In Javascript, "goto" may be a reserved word, but it doesn't actually do anything...
NickFitz
Downvoted, same reason as NickFitz.
Tim Down
A: 

Use function for multilevel loops - this is good way:

function find_dup () {
    for (;;) {
        for(;;) {
            if (done) return;
        }
    }
}
Anatoliy