Label statement in Javascript

Source: Internet
Author: User
This article mainly analyzes the Label statements in Javascript through instance comparison, which has good reference value. If you need a friend, let's take a look at the Label statement. The syntax described in the book is:

For example, begin: for (var I = 0; I <10; I ++) {alert (I );}

A typical example shows the Label application: (no Label is 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++;   } }

Alert (num); // The loop jumps out of the j loop when I is 5 and j is 5, but continues to execute the I loop and outputs 95

Compare the program after Label is used: (After Label is added)

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); // The loop is 5 in I, when j is set to 5, the system jumps out of the double loop and returns to the outPoint layer for further execution. The output is 55.

The break and continue statements are used for comparison:

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

From the value of alert (num), we can see that the function of the continue outPoint; statement is to jump out of the current loop and jump to the for loop under the outPoint (TAG) to continue execution.

The above is all the content of this article. I hope this article will help you in your study or work, and I also hope to support PHP!


For more information about the Label statements in Javascript, see the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.