Java-break Jump Statement

Source: Internet
Author: User

When you develop code, you often wonder: How does a break jump-out statement apply?

There are two scenarios for using break: One, the switch statement. Second, the circular statement.

The switch statement is not introduced here, mainly the application of break in the loop.

 for (int i=0; i<5; i++) {    if(i = = 0) {    System.out.println (i)    ;    Break ;    }}
SYSTEM.OUT.PRINTLN ("Break Test");

This code indicates that when i=0, the output 0,break statement exits the loop directly.

The result is:

0 BreakTest

The above is in a single cycle of the application scenario, we will also encounter multiple loops when the situation, then break will jump out of which loop?

 for (int j=0; j<5; j + +) {    for (int i=0; i<5; i++) {      if( i = = 0) {        System.out.println (i)        ;    Break ;//(1)    }    }    System.out.println ("Jump out of Layer 1 for loop here");     if (j = = 0) {    System.out.println ("Terminator");        Break ; //(2)    }}

This is a double-loop example, (1) where break jumps out of the inner Loop, (2) at the break out of the outer loop. In other words, break can only jump out of a 1-layer loop. This example uses two break to jump out of a double loop, and if only 1 break how do I skip to the outermost loop? Look at the following code:

First:for (int j=0; j<5; j + +) {    Second:for (int i=0; i<5; i++ {        if(i = = 0) {            System.out.println (i);              Break First ;        }    }    System.out.println ("Jump out of Layer 1 for loop here");     if (j = = 0) {        System.out.println ("Terminator");          Break ;    }}

Here I just need to give each cycle a name, so that you can jump to which loop you want him to jump to.

See here you learned the break statement?

Java-break Jump Statement

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.