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