Created
May 9, 2012 23:00
-
-
Save yuxel/2649559 to your computer and use it in GitHub Desktop.
labeled statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//normal loop | |
for (var i=0; i < 3; i++) { | |
for(var j = 0; j < 3; j++) { | |
console.log(i,j); | |
} | |
} | |
/* output for normal loop | |
0 0 | |
0 1 | |
0 2 | |
1 0 | |
1 1 | |
1 2 | |
2 0 | |
2 1 | |
2 2 | |
*/ | |
//normal loop with break on inner loop | |
for (var i=0; i < 3; i++) { | |
for(var j = 0; j < 3; j++) { | |
if (i == 1 && j==1) break; | |
console.log(i,j) | |
} | |
} | |
/* output for break on inner loop | |
0 0 | |
0 1 | |
0 2 | |
1 0 //note that we dont have 1 1 and 1 2 | |
2 0 | |
2 1 | |
2 2 | |
*/ | |
// break to 'labeled statement' | |
foobar : for (var i=0; i < 3; i++) { | |
for(var j = 0; j < 3; j++) { | |
if (i == 1 && j==1) break foobar; //break to 'foobar' labeled statement | |
console.log(i,j) | |
} | |
} | |
/* output for break to labeled statement is | |
0 0 | |
0 1 | |
0 2 | |
1 0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment