In relatively complex business logic, we often use multiple loops to determine a condition in the innermost layer. Once the condition does not match, the break can be used directly.
This will certainly have an impact on performance, and may also affect business judgment. The best way is to jump out of all cycles and return to the outermost layer. This is similar to the Goto syntax criticized by the masses.
As we all know, Java does not support the Goto syntax, although the goto keyword is reserved. There is actually a simple solution to this type of problem.
Java code
- Public void showexample (){
- Breakfor: For (INT I = 0; I <100; I ++ ){
- For (Int J = 0; j <100; j ++ ){
- If (J % 15 = I ){
- Break breakfor;
- }
- }
- }
- }
public void showExample() {breakFor: for (int i = 0; i < 100; i++) {for (int j = 0; j < 100; j++) {if (j % 15 == i) {break breakFor;}}}}
Use any string, such as breakfor In the example, to directly break the AK to the outermost layer.
This syntax is simple and practical ....