JavaScript提供了完全控制来处理循环和switch语句。在某些情况下,您可能需要走出循环而不到达最低点。当您想跳过一部分代码块并开始循环的下一次迭代时,可能还会出现这种情况。 为了处理所有这种情况,JavaScript提供了break和继续语句。这些语句分别用于立即退出任何循环或开始任何循环的下一个迭代。 中断声明在休息的语句,这是简单地用引入的开关语句是用来提前退出循环,封闭大括号的爆发。 流程图break语句的流程图如下所示: ![]() 例以下示例说明了带有while循环的break语句的用法。请注意,一旦x达到5并到达紧接大括号下方的document.write(..)语句,循环将如何提前中断-
现场演示
<html> <body> <script type = "text/javascript"> <!-- var x = 1; document.write("Entering the loop<br /> "); while (x < 20) { if (x == 5) { break; // breaks out of loop completely } x = x + 1; document.write( x + "<br />"); } document.write("Exiting the loop!<br /> "); //--> </script> <p>Set the variable to different value and then try...</p> </body> </html> 输出量Entering the loop 2 3 4 5 Exiting the loop! Set the variable to different value and then try... 我们已经在switch语句中看到了break语句的用法。 继续声明在继续语句告诉口译员立即启动循环的下一次迭代,并跳过余下的代码块。当遇到continue语句时,程序流程立即移至循环检查表达式,如果条件仍然为真,则它将开始下一次迭代,否则控件退出循环。 例这个例子说明了如何使用一个继续语句while循环。请注意,当变量x中的索引达到5 时,如何使用continue语句跳过打印-
现场演示
<html> <body> <script type = "text/javascript"> <!-- var x = 1; document.write("Entering the loop<br /> "); while (x < 10) { x = x + 1; if (x == 5) { continue; // skip rest of the loop body } document.write( x + "<br />"); } document.write("Exiting the loop!<br /> "); //--> </script> <p>Set the variable to different value and then try...</p> </body> </html> 输出量Entering the loop 2 3 4 6 7 8 9 10 Exiting the loop! Set the variable to different value and then try... 使用标签控制流程从JavaScript 1.2开始,标签可以使用break并继续更精确地控制流程。甲标签是只是一个标识符,后跟一个冒号(:)被施加到一个语句或代码块。我们将看到两个不同的示例,以了解如何使用带有break和continue的标签。 注意 -'continue '或'break'语句与其标签名称之间不允许换行。另外,标签名称和关联的循环之间不应有任何其他语句。 请尝试以下两个示例,以更好地理解标签。 例子1以下示例显示如何使用break语句实现Label。
现场演示
<html> <body> <script type = "text/javascript"> <!-- document.write("Entering the loop!<br /> "); outerloop: // This is the label name for (var i = 0; i < 5; i++) { document.write("Outerloop: " + i + "<br />"); 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 document.write("Innerloop: " + j + " <br />"); } } document.write("Exiting the loop!<br /> "); //--> </script> </body> </html> 输出量Entering the loop! 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 Exiting the loop! 例子2
现场演示
<html> <body> <script type = "text/javascript"> <!-- document.write("Entering the loop!<br /> "); outerloop: // This is the label name for (var i = 0; i < 3; i++) { document.write("Outerloop: " + i + "<br />"); for (var j = 0; j < 5; j++) { if (j == 3) { continue outerloop; } document.write("Innerloop: " + j + "<br />"); } } document.write("Exiting the loop!<br /> "); //--> </script> </body> </html> 输出量Entering the loop! Outerloop: 0 Innerloop: 0 Innerloop: 1 Innerloop: 2 Outerloop: 1 Innerloop: 0 Innerloop: 1 Innerloop: 2 Outerloop: 2 Innerloop: 0 Innerloop: 1 Innerloop: 2 Exiting the loop! |