[removed] confused about how nested for loops work

前端 未结 5 890
傲寒
傲寒 2020-12-17 02:49

Why do nested for loops work in the way that they do in the following example:

var times = [
            [\"04/11/10\", \"86kg\"], 
                     


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 03:13

    // remember that the increment of the counter variable
    // is always executed after each run of a loop
    
    
    for (var i = 0; i < n; i++) {
        // some statement(s) to do something.. 
    
            // initializes child-loop counter in the first run of the parent-loop
            // resets child-loop counter in all following runs of the parent-loop
            // while i is greater than 0 and lower than n
    
        for (var j = 0; j < p; j++) {
            // some statement(s) to do something..
    
                // initializes grandchild-loop counter in the first run of the child-loop
                // resets grandchild-loop counter in all following runs of the child-loop
                // while j is greater than 0 and lower than p
    
            for (var k = 0; k < q; k++) {
                // some statement(s) to do something..
                // or add more internal loop-nestings if you like..
            }
        }
    }
    
    // if the counter variables of the descendent-loops were set before the loop-nesting,
    // the inner loops would only run once, because the counter would keep the value
    // of the abortion condition after the loop is finished
    

提交回复
热议问题