In Java, to jump out of multiple loops, you can define a label in front of the outer loop statement, and then use a labeled 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;
}
}
In addition, I personally do not usually use the label this way, but let the outer loop condition expression results can 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
}
}
}
How to jump out of the current multiple nested loops in Java