1. 程式人生 > 其它 >JS基礎12--break和continue

JS基礎12--break和continue

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="">
    <script src=""></script>
</head>

<body>

</body>
<script>
    /* 
        break和continue
        -break:
        break用來終止Switch和迴圈語句;
        break執行後當前的Switch或迴圈會立刻終止;
        break會終止離它最近的迴圈
        -continue:
        continue用來跳過當次迴圈
    */
    for (let i = 0; i < 5; i++) {
        console.log(i)

        if (i == 3) {
            break //break雖然寫在if裡,但是是對迴圈生效的,不是對if生效的
            console.log('跳出了')

        }
    }
    for (let i = 0; i < 5; i++) {

        for (let j = 0; j < 5; j++) {
            if (j == 2) {
                break //break雖然寫在if裡,但是是對迴圈生效的,不是對if生效的
                console.log('跳出了')
            }
            console.log('內層迴圈==>'+j)
        }
        console.log(i)
    }
    for (let i = 0; i < 5; i++) {

        if (i == 3) {
            continue //continue雖然寫在if裡,但是是對迴圈生效的,不是對if生效的
            console.log('跳出了')

        }
        console.log('continue'+i)

    }
</script>

</html>