1

I was trying to find a way to break nested loops better than this way

 var found = false;
  for(x=0; x<10; x++){
    for(i=0; i<10; i++){
        if(i===3){found =true; break;}; //break inner loop
    };//end of inner loop (i)
   if(found){break;}; // break outer loop
  };//end of outer loop for(x)

then I found an answer here recommends to use labels to avoid more breaks and a Boolean like this

 //var found = false; // - using labels we saved this Boolean 
  exitlabel:
  for(x=0; x<10; x++){
    for(i=0; i<10; i++){
        if(i===3){break exitlabel;}; //jump to the label
    };//end of inner loop (i)
   //if(found){break;}; // - using labels we SAVED this if statement
  };//end of outer loop for(x)

but the problem is I can't but the label after the 2 loops to quit them . when I do this the browser tells me it can't find the label ! like this:

  for(x=0; x<10; x++){
    for(i=0; i<10; i++){
        if(i===3){break exitlabel;};
    };
  };
  exitlabel:  // ERROR

and butting the label before the loops will make the loops execute again from the beginning , unless I use a if statement and a Boolean right after the label and before the loops to bypass the 2 loops, with something like this

 var found = false;
 exitlabel:
 if(!found){
     for(x=0; x<10; x++){
       for(i=0; i<10; i++){
           if(i===3){found =true; break exitlabel;};
       };//end of inner loop (i)
     };//end of outer loop for(x)
 };// end of if statement

IS there a way to save a Boolean and avoid wrapping my loops inside a IF statement like this ,and just put the label after the 2 loops like C language?

if this is a stupid question , please consider I'm very newbie, thanks.

Community
  • 1
  • 1
Accountant م
  • 6,975
  • 3
  • 41
  • 61
  • 2
    wrap everything inside a function and use return instead of break? That way the return is in scope of whatever you want to return. Also, have a look at Array.some() or Array.every() . It stops looping once a value returns as true / false. – Shilly May 09 '16 at 11:59
  • 1
    @Shilly you beat me by few secs. [JSFiddle](https://jsfiddle.net/RajeshDixit/8ve4fuxo/) – Rajesh May 09 '16 at 12:00
  • 1
    Why do you think putting the label before the loops makes them execute again? That's just not the case. `break` isn't `goto` –  May 09 '16 at 12:02

1 Answers1

3

Is there a way to put a label AFTER all nested loops to break them all in one step?

By using the break statement as you already are, you can break the outer loop. It doesn't need to come after. This makes it halt immediately when you use break exitlable.

A simple test reveals this to be the case.

exitlabel:
  for (x = 0; x < 10; x++) {
    for (i = 0; i < 10; i++) {
      document.querySelector("pre").textContent += " " + i
      if (i === 3) {
        break exitlabel;
      }
    }
  }

  document.querySelector("pre").textContent += " after loop";
<pre></pre>

and butting the label before the loops will make the loops execute again from the beginning

That's not true. By using break and referencing the label attached to the outer for statement, you're telling that loop to halt. It does not tell it to restart from the beginning.

What you're describing sounds more like goto, which JavaScript does not support.

  • ah , I'm sorry, in my code I had a big loop wrapped these 2 small loops and it was executing the 2 loops over and over , so I thought it is because the label was before the loops like `goto` in C , thanks for your time – Accountant م May 09 '16 at 12:25