As we all know, in Java, if you want to jump out of the for Loop, there are generally two methods: break and continue.
The break jumps out of the current for loop, as shown in the following code:
[Java]
Package com. xtfggef. algo;
Public class RecTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
For (int I = 0; I <10; I ++ ){
If (I = 5 ){
Break;
}
System. out. print (I + "");
}
}
}
Output: 0 1 2 3 4
That is to say, the break will jump out (terminate) The current loop.
Continue jumps out of the current loop to enable the next loop, as shown below:
[Java]
Package com. xtfggef. algo;
Public class RecTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
For (int I = 0; I <10; I ++ ){
If (I = 5 ){
Continue;
}
System. out. print (I + "");
}
}
}
Output: 0 1 2 3 4 6 7 8 9
The above two methods cannot jump out of a multi-layer loop. If you need to jump out of a multi-layer loop, you need to use a label, define a label, And Then skip
Use the break label. The Code is as follows:
[Java]
Package com. xtfggef. algo;
Public class RecTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
Loop: for (int I = 0; I <10; I ++ ){
For (int j = 0; j <10; j ++ ){
For (int k = 0; k <10; k ++ ){
For (int h = 0; h <10; h ++ ){
If (h = 6 ){
Break loop;
}
System. out. print (h );
}
}
}
}
System. out. println ("\ nI'm here! ");
}
}
Output:
012345
I'm here!
Obviously!