JavaScript循环控制


JavaScript提供了完全控制来处理循环和switch语句。在某些情况下,你可能需要走出循环而不到达最低点。当你想跳过一部分代码块并开始循环的下一次迭代时,可能还会出现这种情况。

为了处理所有此类情况,JavaScript提供了breakcontinue语句。这些语句分别用于立即退出任何循环或开始任何循环的下一个迭代。

break声明


break声明,该声明与 switch 语句,用于提前退出循环,打破包围的花括号。

以下示例说明了break带while循环的语句。请注意,循环如何尽早爆发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;   //完全脱离循环
                }
                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...

我们已经看到了break内部声明一个switch语句。

continue声明


continue语句告诉解释器立即开始循环的下一个迭代,并跳过剩余的代码块。当一个continue遇到语句,程序流立即移至循环检查表达式,如果条件仍然为真,则它将开始下一次迭代,否则控件退出循环。

此示例说明了continue带while循环的语句。请注意continue当索引保存在变量中时,该语句用于跳过打印x达到5:

<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;   //跳过循环主体的其余部分
                    }
                    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开始,标签可以与breakcontinue以更精确地控制流程。一种label只是一个标识符,后跟一个冒号(:),该冒号用于语句或代码块。我们将看到两个不同的示例,以了解如何使用带有break和continue的标签。

注意:之间不允许有换行符'continue'或者'break'语句及其标签名称。同样,在标签名称和关联的循环之间不应有任何其他语句。

请尝试以下两个示例,以更好地理解标签。

例子1

以下示例显示了如何使用break语句实现Label。

<html>
    <body>
        <script type = "text/javascript">
            <!--
                document.write("Entering the loop!<br /> ");
                outerloop:        //这是标签名称
                for (var i = 0; i < 5; i++) {
                    document.write("Outerloop: " + i + "<br />");
                    innerloop:
                    for (var j = 0; j < 5; j++) {
                        if (j > 3 ) break ;           //退出最里面的循环
                        if (i == 2) break innerloop;  //  做同样的事情
                        if (i == 4) break 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 
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:     //这是标签名称
         
            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!