Skip to content

Instantly share code, notes, and snippets.

@yuxel
Created May 9, 2012 23:00
Show Gist options
  • Save yuxel/2649559 to your computer and use it in GitHub Desktop.
Save yuxel/2649559 to your computer and use it in GitHub Desktop.
labeled statements
//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