Java jumps out of multiple loops
Scenario: A lot of time need to do out of multiple loops, and in Java although after the goto keyword, but is reserved word, and not enabled.
A return that terminates the current code does not fulfill the business requirement. All you can use is a break, but a break only breaks out of the current loop.
private static void testBreak(a) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println("testBreak==j==" + j);
if (j == 2)
break;
}
System.out.println("testBreak==i====>>>" + i);
}
System.out.println("testBreak======>>>END");
}
Copy the code
Results:
testBreak==j==0
testBreak==j==1
testBreak==j==2
testBreak==i====>>>0
testBreak==j==0
testBreak==j==1
testBreak==j==2
testBreak==i====>>>1
testBreak==j==0
testBreak==j==1
testBreak==j==2
testBreak==i====>>>2
testBreak======>>>END
Copy the code
Conclusion: All executions terminate at j==2 but only break out of the current loop.
Solution 1: Continue to use break, but add a flag as the condition, and each time the condition is checked, the loop and flag are checked.
Code:
private static void outloopByBreak(a) {
boolean flag = true;
for (int i = 0; i < 3 && flag; i++) {
for (int j = 0; j < 5; j++) {
System.out.println("outloopByBreak==j==" + j);
if (j == 2) {
flag = false;
break;
}
}
System.out.println("outloopByBreak==i====>>>" + i);
}
System.out.println("outloopByBreak======>>>END");
}
Copy the code
Results:
outloopByBreak==j==0
outloopByBreak==j==1
outloopByBreak==j==2
outloopByBreak==i====>>>0
outloopByBreak======>>>END
Copy the code
Conclusion: can solve the problem of jumping out of the loop, but will continue to execute after the completion of the internal loop, until the next judgment will jump out of the loop.
Solution 2: There is a use of Break in Java that does something similar to goto by adding a break flag before the loop to be broken; It can be done out of the loop (recommended).
Code:
private static void outloopByBreakLikeGoto(a) {
flag: for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println("outloopByBreakLikeGoto==j==" + j);
if (j == 2) break flag;
}
System.out.println("outloopByBreakLikeGoto==i====>>>" + i);
}
System.out.println("outloopByBreakLikeGoto======>>>END");
}
Copy the code
Results:
outloopByBreakLikeGoto==j==0
outloopByBreakLikeGoto==j==1
outloopByBreakLikeGoto==j==2
outloopByBreakLikeGoto======>>>END
Copy the code
Conclusion: The effect similar to GOto is achieved
Solution 3: Try… Catch mode can also be used to break out of multiple loops
Code:
private static void outloopByTryCatch() { try { for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { System.out.println("outloopByTryCatch==j==" + j); if (j == 2) throw new Exception(); } System.out.println("outloopByTryCatch==i====>>>" + i); }} catch (Exception e) {system.out.println ("====>>outloopByTryCatch "); } System.out.println("outloopByTryCatch======>>>END"); }Copy the code
Results:
OutloopByTryCatch ==j==0 outloopByTryCatch==j==1 outloopByTryCatch==j==2 ====>>outloopByTryCatch jumps out outloopByTryCatch======>>>ENDCopy the code
Conclusion: Throwing exceptions is not recommended