What is js | label and what is its use in the loop

Looking at the third chapter of the third edition of advanced JavaScript programming, I found that the label statement was not met in the normal learning of js. After learning and practice, the combination of label, break and continue can save some steps, and the nonsense is not much to say directly on the code.

Let's start with a normal for loop:

 var num = 0;
        for (var i = 0 ; i < 10 ; i++){
             for (var j = 0 ; j < 10 ; j++){
                num++;
             }
        }
        alert(num); // 100

After double-layer circulation, num is 100, so there should be no problem here

Join break:
 

var num = 0;
        for (var i = 0 ; i < 10 ; i++){
            console.log("i="+i)
             for (var j = 0 ; j < 10 ; j++){
                console.log("j="+j) 
                  if( i == 5 && j == 5 ){
                        console.log("i,j=5")
                        break;
                  }
                num++;//95             
             }
        }
        console.log(num);

Loop jumps out of J loop when i is 5 and j is 5, but i loop will continue to execute, so num has five times less + + number, output 95

Join continue:
 

 var num = 0;
        for (var i = 0 ; i < 10 ; i++){
            console.log("i="+i)
             for (var j = 0 ; j < 10 ; j++){
                console.log("j="+j) 
                  if( i == 5 && j == 5 ){
                        console.log("i,j=5")
                        continue;
                  }
                num++;//99             
             }
        }
        console.log(num);

Output is 99, skip when i=5, j=5, but continue the operation of j=6, so num is missing only once++

The above two examples show that under normal circumstances, break and continue are added, and two different jump cycles are added. Label, on the other hand, adds a label to the code, which is used in combination with break and continue to return a specific location in the code.

Add a label to the statement with break:

 var num = 0;
        outPoint://Here is the label, and the label name is outPoint
        for (var i = 0 ; i < 10 ; i++){
            console.log("i="+i)
            for (var j = 0 ; j < 10 ; j++){
                console.log("j="+j) 
                if( i == 5 && j == 5 ){
                        console.log("i,j=5")
                        console.log(num) 
                        break outPoint;
                }
            num++;//55
            }
        }
        console.log(num); 

When i is 5 and j is 5, the loop will jump out of the double loop and return to the outlier layer to continue the next operation except the loop. Output 55

Add label to statement with continue

 var num = 0;
        outPoint:
        for (var i = 0 ; i < 10 ; i++){
            console.log("i="+i)
            for (var j = 0 ; j < 10 ; j++){
                console.log("j="+j) 
                if( i == 5 && j == 5 ){
                        console.log("i,j=5")
                        console.log(num) 
                        continue outPoint;
                }
            num++;//95
            }
        }

Output 95, when i is 5 and j is 5, the loop will jump out of the double loop, and return to the outlier layer to continue to execute the unfinished loop, that is, from i=6, num only skips five times++

 

It should be noted that excessive use of label will bring trouble to debugging. Descriptive label should be used and too many loops should not be nested at the same time.

Tags: Javascript Programming less

Posted on Fri, 31 Jan 2020 14:24:05 -0800 by inerte