Javascript for loop specifies anchor point jumps

Source: Internet
Author: User

In some cases where multiple nested for loops are used

Break and continue will be used to jump the loop.

Break is jumping out of the loop

Continue is to jump out of the current loop and continue the next loop

The multi-layer for loop nesting uses these two keywords to skip to the previous layer of the current for loop by default

What if we need to jump to the upper level of the previous layer, or jump to the top layer?

JavaScript provides us with a looping tab that feels a bit like a goto statement in C

1 Here :2   for(vari=0;i<5;i++){3    for(varj=0;j<5;j++){4    if(i===2&&j===2){5       BreakHere ;6    }7 Console.log (i,j);8   }9}

Here in the code above can define its own name, and it's important to see that there's a colon behind here, not a semicolon! Run to discover the output
0,00,10,20,30,41,01,11,21,31,42,02,1

You can see that when you loop to i=2,j=2, you exit the loop directly instead of just exiting the inner loop for a better contrast, and if we remove the here run behind the break here, we can see the output
0,00,10,20,30,41,01,11,21,31,42,02,13,03,1// omit 3,2~4,3

At this point, the break simply jumps out of the inner loop, and the function continues to circulate from the outer loop i=3. This output obviously has the same effect as continue here (break) The former is jumping out of the current layer loop, and the outer loop continues (continue here) The latter is to jump to the outermost loop to continue

Javascript for loop specifies anchor point jumps

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.