Java:label use (For loop)

Source: Internet
Author: User

1  Packagejava_test;2 3  Public classLabeledfor {4 5      Public Static voidMain (String[]args) {6         intI=0;7Outer//cannot has statement here8              for(;true;){9 Inner:Ten                      for(; i<10;i++){ OneSystem.out.println ("i=" +i); A                         if(i==2){ -System.out.println ("Continue"); -                             Continue; the                         } -                         if(i==3){ -System.out.println ("Break"); -i++;//Otherwise I never gets incremented +                              Break; -                         } +                         if(i==7){ ASYSTEM.OUT.PRINTLN ("Continue outer"); ati++; -                             Continueouter; -                         } -                         if(i==8){ -SYSTEM.OUT.PRINTLN ("Break outer"); -                              Breakouter; in                         } -                          for(intk=0;k<5;k++){ to                             if(k==3){ +SYSTEM.OUT.PRINTLN ("Continue inner"); -                                 ContinueInner; the                             } *                         } $                     }Panax Notoginseng             } -             //cannot break or continue-labels here the     } +}

Output

1i= 02 ContinueInner3I= 14 ContinueInner5I= 26 Continue7I= 38  Break9I= 4Ten ContinueInner OneI= 5 A ContinueInner -I= 6 - ContinueInner theI= 7 - Continueouter -I= 8 -  BreakOuter

Usage:

1 Label1:2outer-iteration{3inner-iteration{4         //...5          Break;//(1)6         //...7         Continue;//(2)8         //...9         ContinueLabel1;//(3)Ten         //... One          BreakLabel1;//(4) A     } -}

In (1), the break breaks out of the inner iteration and your end up in the out iteration.

In (2), the continue moves back to the beginning of the inner iteration.

But in (3), the continue Label1 breaks out of the inner iteration and the outer iteration,all the the- Label1. Then it does in fact continue the iteration,but starting at the outer iteration.

In (4), the "break" Label1 also breaks all the The "the" to Label1, but it does not reenter the iteration. It actually does break out of both iterations.

Java:label use (For loop)

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.