one, the marking way
In Java, to jump out of multiple loops, you can define a label before the loop statement outside, and then use the label break statement in the code of the inner loop body to jump out of the outer loop. For example:
OK:
for (int i=0;i<10;i++) {
for (int j=0;j<10;j++) {
System.out.println ("i=" + i + ", j=" + j);
if (j = = 5) break OK;
}
second, break out of the current cycle, through the internal jump out of the condition control outside the loop
for (int i=0;i<4;i++) {for
(int j=0;j<5;j++) {
System.out.println ("i=" +i+ "; j=" +j);
if (j==3) {
i=4;
break;
}}}
three, throw the exception can also jump out of multiple loops
try {for
(int i = 0; i < 4; i++) {for
(int j = 0; J < 5; J +) {
System.out.println ("i=" + i +); j= "+ j);
if (j = = 3) {
throw new Exception ();}} \
catch (Exception e) {
System.out.println ("E") ;
}
Typically, you don't use the label in this way, but let the result of the outer loop conditional expression be controlled by the inner Loop body code, for example, to find a number in a two-dimensional array.
int arr[][] = {{1,2,3},{4,5,6,7},{9}}; Boolean found = false; for (int i=0;i<arr.length &
&!found;i++) {for (int j=0;j<arr[i].length;j++) {System.out.println ("i=" + i + ", j=" + j);
if (arr[i][j] = = 5) {found = true;
Break }
}
}