【代码笔记】Web-Javascript-javascript break和continue语句

一,效果图。

 

二,代码。

 

复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>javascript break和continue语句</title>
</head>

<body>
    <!--break语句-->
    <p>点击按钮,测试带有brak语句的循环</p>
    <button onclick="myFunction()">点击这里</button>
    <p id="demo"></p>
    <script>
    function myFunction() {
        var x = "",
            i = 0;
        for (i = 0; i < 10; i++) {
            if (i == 3) {
                break;
            }
            x = x + "the number is" + i + "<br>";
        }
        document.getElementById("demo").innerHTML = x;
    }
    </script>
    <!--continue语句-->
    <p>点击下面按钮来执行循环,该循环会跳过i=3的步进</p>
    <button onclick="myFunction()">点击这里</button>
    <p id="demo"></p>
    <script>
    function myFunction() {
        var x = "",
            i = 0;
        for (i = 0; i < 10; i++) {
            if (i == 3) {
                continue;
            }
            x = x + "the number is" + i + "<br>";
        }
        document.getElementById("demo").innerHTML = x;
    }
    </script>
    <!--javascript标签-->
    <script>
    cars = ["BMW", "volvo", "Saab", "Ford"];
    list: {
        document.write(cars[0] + "<br>");
        document.write(cars[1] + "<br>");
        document.write(cars[2] + "<br>");
        break list;
        document.write(cars[3] + "<br>");
        document.write(cars[4] + "<br>");
        document.write(cars[5] + "<br>");
    }
    </script>
</body>

</html>
复制代码

 

 

参考资料:《菜鸟教程》

posted @ 2018-12-24 09:11  花儿迎风笑  阅读(230)  评论(0编辑  收藏  举报