Use of jump statements such as break, continue, and tags in java [top]

Source: Internet
Author: User

The jump statements in Java use Break, continue, and tags to perform their functions individually or in combination.

Today, I encountered a topic on the label naming specification, and by the way the jump statement syntax has been read, very rewarding.

In the book "Java Programming Ideas", the author explains in detail that the case code is now written below:

 Public classLabeledfor { Public Static voidMain (string[] args) {intI=0; Outer: for(;true;) {inner: for(; i<10;i++) {System.out.println ("I=" +i); if(i==2) {System.out.println ("Continue"); Continue; }                        if(i==3) {System.out.println ("Break"); I++;//Otherwise I never gets incremented                             Break; }                        if(i==7) {System.out.println ("Continue outer"); I++;//Otherwise I never gets incremented                            Continueouter; }                        if(i==8) {System.out.println ("Break Outer");  Breakouter; }                         for(intk=0;k<5;k++){                            if(k==3) {System.out.println ("Continue inner"); Continueinner; }                        }                    }            }    }}

See the results of the operation:

I=0continue  inneri=1continue  inneri=2continuei=3 break  i=4continue  inneri=5continue  inneri=6Continue inneri=7continue  outeri=8 break outer

The more important thing to think about is thatbreak interrupts the for loop and is before the end of the for loop, that is, the increment expression will not execute. Continue does not skip incrementing statements.

Similarly, in this case, when i==7, the continue outer statement jumps to the top of the loop and also skips the increment.

If there is no break outer, there is no way to jump out of the outer loop from inside the loop. this is because the break itself interrupts only the inner loop (continue is the same).

If you want to interrupt and exit at the same time, simply use a return.

Use of jump statements such as break, continue, and tags in java [top]

Related Article

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.