break与continue语句

1.break:立即退出循环

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>break和continue语句</title>
</head>
<body>
    <h2>break:立即退出循环</h2>
    <script type="text/javascript">
        var num = 0;
        for (var i = 1; i < 15; i++) {
            //如果i是6的倍数,退出
            if(i %6 == 0){
                break;
            }
            // console.log(i);
            //1,2,3,4,5
            num++; //i=1,num=1;i=2,num=2;i=3,num=3,i=4,num=4,i=5,num=5
        }
        console.log(num);//5

    </script>


</body>
</html>

2.continue语句:结束本次循环,继续开始下一次

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>break和continue语句</title>
</head>
<body>
    <h2>continue:结束本次循环,继续开始下一次</h2>
    <script type="text/javascript">
        var num = 0;
        for (var i = 1; i < 15; i++) {
            //如果i是6的倍数
            if(i %6 == 0){
                continue;
            }
            // console.log(i);
            //1,2,3,4,5,7,8,9,10,11,13,14
            num++; 
            /**i=1,num=1;i=2,num=2;i=3,num=3,i=4,num=4,i=5,num=5,i=6,num=5;i=7,num=6,i=8,num=7;i=9,num=8;
            i=10,num=9;i=11,num=10,i=12,num=10,i=13,num=11;i=14,num=12**/
        }
        console.log(num)//12

    </script>


</body>
</html>

continue例子2:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>break和continue语句</title>
</head>

<body>
    <h2>continue:结束本次循环,继续开始下一次</h2>
    <script type="text/javascript">
    for (var s = 0, j = 1; j <= 10; j++) {
        //如果i是5的倍数
        if (j % 5 == 0) {
            continue;
        }
        // console.log(j);
        //1,2,3,4,6,7,8,9
        s+=j;//j=1,s=1;j=2,s=3;j=3,s=6; j=4,s=10;j=5,s=10;j=6,s=16;j=7,s=23;j=8,s=31,j=9,s=40
        
    }
    console.log(s) //40
    </script>
</body>

</html>

continue例子3:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>break和continue语句</title>
</head>

<body>
    <h2>continue:结束本次循环,继续开始下一次</h2>
    <script type="text/javascript">
    //打印所有0-50之间除20和30之外的5的倍数
    for (var n = 0; n <= 50; n+=5) {
        if(n==20 || n==30){
            continue;
        }
        console.log(n);//打印出所有5的倍数;0,5,10,15,20,25,30,35,40,45,50
    }
    </script>
</body>

</html>

 

posted @ 2018-09-16 15:56  前端HL  阅读(217)  评论(0编辑  收藏  举报