如何使用 JavaScript 打破嵌套的 for 循环?
break 语句,用于提前退出循环。
标签可以与中断一起使用,以更精确地控制流程。标签只是一个标识符,后跟一个冒号(:),应用于语句或代码块。
注意:标签名称和相关循环之间不应有任何其他语句。
示例 1:从嵌套循环中中断
Break Nested For loop
输出:
Entering the Geeks For Geeks!
For Upper Level in GfG : 0
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
For Upper Level in GfG : 1
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
For Upper Level in GfG : 2
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
For Upper Level in GfG : 3
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
Exiting the Geeks For Geeks!
示例 2:使用Labels从嵌套循环中中断。
Break Nested For loop Using Labels
输出:
Entering the Geeks for Geeks!
For Upper Level in GfG : 0
For Deeper Level in GfG: 0
For Deeper Level in GfG: 1
For Deeper Level in GfG: 2
For Deeper Level in GfG: 3
For Upper Level in GfG : 1
For Deeper Level in GfG: 0
For Deeper Level in GfG: 1
For Deeper Level in GfG: 2
For Deeper Level in GfG: 3
For Upper Level in GfG : 2
For Upper Level in GfG : 3
Exiting the Geeks For Geeks!