Continue语句告诉解释器立即开始循环的下一个迭代,并跳过剩余的代码块。break语句用于提早退出循环,使之脱离封闭的花括号。
当遇到continue语句时,程序流将立即移至循环检查表达式,如果条件仍然为真,则它将开始下一次迭代,否则,控件将退出循环。
您可以尝试运行以下命令,以了解如何使用JavaScript中的continue语句。此示例说明了while循环内continue语句的用法。请注意,当变量x中的索引达到5时,如何使用continue语句跳过打印-
<html> <body> <script> 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> </body> </html>