Question about loops and continue

后端 未结 4 2056
轮回少年
轮回少年 2021-01-06 10:12

So is this actually naming the loop to nextLoop? So when it says continue nextLoop, does it go back to the top right away?

   var t         


        
4条回答
  •  离开以前
    2021-01-06 10:40

    other languages let you break out of a selected number of inner loops, if(!x)break 2; would continue the process at a point two steps up.

    I've seen complex loops with multiple loop labels, and continues called to a specific label, but I agree it can confuse the logic.

    You can almost always improve the efficiency of a loop by writing it without the continue:

    var total= 0;
    nextLoop: 
    for (var i = 0; i < 7; ++i ){
        for (var j = 0; j < 6 ; ++j ){
            if(i>4) total++;
        }
        total++;
    }
    
    total++; 
    

提交回复
热议问题