Java jumps out of multiple loop tip

Source: Internet
Author: User
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  
  1. Public void showexample (){
  2. Breakfor: For (INT I = 0; I <100; I ++ ){
  3. For (Int J = 0; j <100; j ++ ){
  4. If (J % 15 = I ){
  5. Break breakfor;
  6. }
  7. }
  8. }
  9. }
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 ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.