Home > Web Front-end > JS Tutorial > A brief discussion of Label statements in Javascript

A brief discussion of Label statements in Javascript

高洛峰
Release: 2016-12-29 09:16:08
Original
1683 people have browsed it

Label statement, according to the syntax in the book is:

Label: statement

如: begin: for (var i = 0; i < 10 ; i++ ){
     alert(i);
   }
Copy after login

For a typical example, see After finishing, you will understand the application of Label: (No Label added)

var num = 0;
 for (var i = 0 ; i < 10 ; i++){
   for (var j = 0 ; j < 10 ; j++){
    if( i == 5 && j == 5 ){
     break;
    }
   num++;
   }
 }
Copy after login

alert(num); // The loop will jump out of the j loop when i is 5 and j is 5, but it will continue to execute the i loop and output 95

Comparison of the program after using Label: (after adding Label)

var num = 0;
outPoint:
for (var i = 0 ; i < 10 ; i++){
  for (var j = 0 ; j < 10 ; j++){
   if( i == 5 && j == 5 ){
    break outPoint;
   }
  num++;
  }
}
alert(num); // 循环在 i 为5,j 为5的时候跳出双循环,返回到outPoint层继续执行,输出 55
Copy after login

Compare the use of break and continue statements:

var num = 0;
  outPoint:
  for(var i = 0; i < 10; i++)
  {
   for(var j = 0; j < 10; j++)
   {
    if(i == 5 && j == 5)
    {
     continue outPoint;
     }
     num++;
   }
  }
  alert(num); //95
Copy after login

As can be seen from the value of alert(num), the function of the continue outPoint; statement is to jump out of the current loop and jump to outPoint ( The for loop under the label) continues to execute.

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support the PHP Chinese website!


For more articles related to the Label statement in Javascript, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template