What's the best way to break from nested loops in JavaScript?
Just like Perl,
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
}
}
}
as defined in EMCA-262 section 12.12. [MDN Docs]
Unlike C, these labels can only be used for continue
and break
, as Javascript does not have goto
.
How to break nested loops in JavaScript?
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;
}
}
}
}
Break statement not exiting all nested loops within JavaScript
You have to specify which loop you are breaking from.
loop1: for (i = 0; i < 5; i++) {
loop2: for (j = 0; j < 5; j++) {
if (i == 3) {
break loop1;
}
document.write(i + '*' + j + '<br>');
}
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break
How do I break out of nested loops in Java?
Like other answerers, I'd definitely prefer to put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.
You can use break
with a label for the outer loop. For example:
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
This prints:
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done
Breaking out from nested for loops
Have a flag and if the flag falls break out of both of them.
var flag = true;
for(condition && flag)
{
for (condition && flag)
{
if(condition) {
flag = false;
break;
}
}
}
Breaking out of nested loops: return or label/break?
After some testing (via Chrome console, MBP 2013, OSX 10.9, Intel i7 @ 2.8GHz, 16GB DDR3), the results are very interesting. I ran two types of tests. The first tested using return and label/break to break out of a nested loop. The second used a straight return and label/break, with nothing else in the function. The test code:
function r() {
for(var i = 0; i < 10; ++i) {
for(var j = 0; j < 10; ++j) {
if(i*j == 50) {
return;
}
}
}
}
function b() {
dance:
for(var i = 0; i < 10; ++i) {
for(var j = 0; j < 10; ++j) {
if(i*j == 50) {
break dance;
}
}
}
}
function r2() {
return;
}
function b2() {
dance:
break dance;
}
var startTime;
var endTime;
console.log("Return test");
startTime = Date.now();
for(var i = 0; i < 1000000000; ++i) {
r2();
}
endTime = Date.now();
console.log(endTime - startTime);
console.log("Break test");
startTime = Date.now();
for(var i = 0; i < 1000000000; ++i) {
b2();
}
endTime = Date.now();
console.log(endTime - startTime);
When comparing breaking out of a the nested loops (functions r() and b() ), the return consistently performed significantly better. However, when using just the return or label/break in the function (functions r2() and b2() ) the label/break performed significantly faster. Test result breakdown:
Test 1, using 10000000 iterations
Average runtime (milliseconds) after 3 runs of using return to leave nested loops: 1215ms
Average runtime (milliseconds) after 3 runs of using label/break to leave nested loops: 1522ms
Test 2, using 1000000000 iterations //2 orders of magnitude more iterations
Average runtime (milliseconds) after 3 runs of using return: 1879ms
Average runtime (milliseconds) after 3 runs of using label/break: 1862ms
Thus:
For breaking nested loops, using return is ~25% faster
For science/HPC, using label/break is ~1% faster
Breaking out of an outer loop from an inner loop in javascript
Depending on what your conditionals are, it should be easy to set the iterator of your for-loop to something that would break it, and set your while condition to false. For example,
while(someBoolean){
for(var i = 0; i < size; i++){
if(wantToBreak){
i = size;
someBoolean = false;
}else{
//Do Stuff
}
}
}
Related Topics
JavaScript Regular Expressions and Sub-Matches
How to Replace Captured Groups Only
Element Not Visible Error (Not Able to Click an Element)
Handle Url Fragment Identifier (Anchor) Change Event in JavaScript
JavaScript - Difference Between Array and Array-Like Object
Injecting a Mock into an Angularjs Service
How to Inherit Old-Style Class from Ecmascript 6 Class in JavaScript
How to Get the Current Time Only in JavaScript
Sum of Array Object Property Values in New Array of Objects in JavaScript
Mobile Safari: JavaScript Focus() Method on Inputfield Only Works with Click
Firebase Query Methods Startat() Taking Case Sensitive Parameters
How to Build Query String with JavaScript