/*
* How do I jump out of the current multiple nested loops in Java?
* In Java, to jump out of multiple loops, you can define a label in front of the outer loop statement,
* Then use the break statement with a label in the code of the inner loop body to jump out of the outer loop
*/
Package Java basic topic; public class Test3 {public static void main (string[] args) {method1 (); Method2 ();} Method one: public static void Method1 () {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;//jumps to an OK out of the loop, i.e. terminates the entire loop}}}}//method two: public static void Method2 () {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;//found 5, so that the outer loop judgment condition becomes false terminates the entire loop break;//jumps out of the current Loop}}}} }
Interview question: How do I jump out of the current multiple nested loops in Java?