美文网首页
JS 循环语句处理

JS 循环语句处理

作者: Cherry丶小丸子 | 来源:发表于2021-09-15 19:36 被阅读0次
    // 利用 JavaScript 语句进行标记,break跳出单层循环
    function recycle0(){
        var result = "";
        result += "recycle0 循环语句标记break例子:" + "\n";
        out:
            for (i = 0; i < 10; i++){
                if (i == 3){
                    break out; // 程序循环终止跳出循环
                }
                result += "数字:" + i + "\n";
            }
        console.log(result)
    }
    recycle0()
    
    // 利用 break 跳出单层循环
    function recycle1(){
        var result = "";
        result += "recycle1 循环break例子:" + "\n";
        for (i = 0; i < 10; i++){
            if (i == 3){
                break; // 程序循环终止跳出循环
            }
            result += "数字:" + i + "\n";
        }
        console.log(result)
    }
    recycle1()
    
    //利用break跳出单层循环
    function recycle2(){
        var result = "";
        result += "recycle2 循环 return 例子:" + "\n";
        console.log(result)
        for (i = 0; i < 10; i++){
            if (i==3){
                return; // 程序循环终止跳出循环
            }
            result= "数字:" + i + "\n";
            console.log(result)
        }
    
    }
    recycle2()
    
    // 利用 continue 绕过条件继续循环
    function recycle3(){
        var result = "";
        result+="recycle3 循环 continue 例子:" + "\n";
        for (i = 0; i < 10; i++){
            if (i == 3){
                continue; // 绕过条件,继续循环
            }
            result += "数字:" + i + "\n";
        }
        console.log(result)
    }
    recycle3()
    
    // 利用 continue 绕过条件继续循环
    function recycle4(){
        var result = "";
        result += "recycle4 循环语句标记 continue 例子:" + "\n";
        out:
            for (i = 0; i < 10; i++){
                if (i == 3){
                    continue out; // 绕过条件,继续循环
                }
                result += "数字:" + i + "\n";
            }
        console.log(result)
    }
    recycle4()
    
    
    // 利用 while 循环
    function recycle5(){
        var result = "";
        result+="recycle5 while 循环break例子:" + "\n";
        var i = 0;
        while(i < 10){
            if (i == 3){
                break;
            }
            result += "数字:" + i + "\n";
            i = i + 1
        }
        console.log(result)
    }
    recycle5()
    
    // while循环(先判断,后执行)
    function recycle6(){
        var result = "";
        result += "recycle6 while循环绕过条件的例子:" + "\n";
        var i = 0;
        while(i < 10){
            if (i != 3){
                result += "数字:" + i + "\n";
            }
            i = i + 1
        }
        console.log(result)
    }
    recycle6()
    
    // do...while循环(先执行,后判断)
    function recycle7(){
        var result = "";
        result += "recycle7 do..while循环绕过条件的例子:" + "\n";
        var i = 0;
        do{
            if (i != 3){
                result += "数字:" + i + "\n";
            }
            i = i + 1
        }
        while(i < 10)
        console.log(result)
    }
    recycle7()
    
    // do...while循环(先执行,后判断)
    function recycle8(){
        var result = "";
        result += "recycle8 do..while循环跳出循环的例子:" + "\n";
        var i = 0;
        do{
            if (i == 3){
                break;
            }
            result += "数字:" + i + "\n";
            i = i + 1
        }
        while(i < 10)
        console.log(result)
    }
    recycle8()
    
    
    // 多重层循环
    function recycle9(){
        var result = "";
        result+="recycle9 多重循环break例子:" + "\n";
        for (i = 0; i < 10; i++){
            for(j = 0; j < 10; j++){
                if (i == 3){
                    break; // 程序跳出内层循环
                }
                result += "数字:(" + i + "-" + j + ")\n";
            }
        }
        console.log(result)
    }
    recycle9()
    
    // 多重层循环
    function recycle10(){
        var result = "";
        result += "recycle10 多重循环break例子:" + "\n";
        for (i = 0; i < 10; i++){
            for(j = 0; j < 10; j++){
                if (j == 3){
                    break; // 程序跳出内层循环
                }
                result += "数字:(" + i + "-" + j + ")\n";
            }
        }
        console.log(result)
    }
    recycle10()
    
    // 多重层循环
    function recycle11(){
        var result = "";
        result += "recycle11 多重循环break例子:" + "\n";
        out:
            for (i = 0; i < 10; i++){
                for(j = 0; j < 10; j++){
                    if (j == 3){
                        break out; // 程序跳出内层循环
                    }
                    result += "数字:(" + i + "-" + j + ")\n";
                }
            }
        console.log(result)
    }
    recycle11()
    
    // 多重层循环
    function recycle12(){
        var result = "";
        result += "recycle12 多重循环break例子:" + "\n";
        for (i = 0; i < 10; i++){
            if (i == 3){
                break; // 程序跳出内层循环
            }
            for(j = 0; j < 10; j++){
                result += "数字:(" + i + "-" + j + ")\n";
            }
        }
        console.log(result)
    }
    recycle12()
    
    // 多重层循环
    function recycle13(){
        var result = "";
        result += "recycle13 多重循环break例子:" + "\n";
        out:
            for (i = 0; i < 10; i++){
                for(j = 0; j < 10; j++){
                    if (i == 3){
                        break out; // 程序跳出外层循环,循环终止
                    }
                    result += "数字:(" + i + "-" + j + ")\n";
                }
            }
        console.log(result)
    }
    recycle13()
    

    相关文章

      网友评论

          本文标题:JS 循环语句处理

          本文链接:https://www.haomeiwen.com/subject/tbrlgltx.html