扫码一下
查看教程更方便
标签可以与 break
和 continue
一起使用,以更精确地控制流。
outerloop: // 这是标签名称 for (var i = 0; i < 5; i++) { console.log("Outerloop: " + i); innerloop: for (var j = 0; j < 5; j++){ if (j > 3 ) break ; // Quit the innermost loop if (i == 2) break innerloop; // Do the same thing if (i == 4) break outerloop; // Quit the outer loop console.log("Innerloop: " + j); } }
成功执行上述代码后会显示以下输出。
Outerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 1
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 2
Outerloop: 3
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 4