Java break and continue implement Goto functionality

Source: Internet
Author: User

 Continue experiment
1 Public classTest {2 Static intI =0;3 Public Static voidMain (string[] args) {4 Lable1:5 while(true){6 7 if(i = = 6){8System.out.println ("Jumping from the Lable2 to the Lable1");9 Break;Ten //continue lable2; One } A - Lable2: - while(true){ the if(i = = 5){ -System.out.println ("Will jump from Lable2 to Lable1"); -i++; - ContinueLable1; + } -i++; +}//Labe2 A}//Lable1 at}//Main -}//class

The upper code has two while loops and is nested, the author sets the Lbale1 in the outer while loop, and sets the Lable2 label on the inner while

When the static variable i =5, the execution

Continue Lable1;
Jumps from the inner loop to the outer loop and executes the outer loop from the beginning

Continue precautions for use:
  
    1. Loop internal jump
    2. Direction up

Error Demo: Non-internal jump

Break experiment

Same as cotinue, but continue jumps to the label and continues execution, and break stops moving at the label

1 Lable1:2      while(true){3 System.out.println (i);4i++;5         6         if(i==3){7System.out.println ("i = 10,break to Lable1");8              BreakLable1;9         }Ten}

Results:

1 02 13 24 i = 10,break to Lable1

It turns out that when executed to i==3, the execution

Break Lable1;
Break to the while () at Lable1. Discovery program no longer executing

                                        Summarize
    1. The same point of continue and break,
    • can implement Goto function (continue lable;break lable)
    • Internal jump (Continue/break statement inside the Loop)
    • Jump up Continue/break after the label must be on the top of the Continue/break statement

Special case: Loop nesting still follows internal jumps and jumps up

2. Different points of ontinue and break  

Continue is jump to the label to continue execution

break jump to stop running at the label

Java break and continue implement Goto functionality

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.