How do I jump out of the current multiple nested loops in Java?

Source: Internet
Author: User

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(intI=0;i<Ten; i++)    {         for(intj=0;j<Ten; j + +) {System. out. println ("i=" + i + ", j=" +j); if(J = =5) BreakOK; }    }  for(intI=0;i<4; i++){                 for(intj=0;j<5; j + +) {System. out. println ("i="+i+"; j="+j); if(j==3) {i=4;  Break; }                }            }

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.

intArr[][] = {{1,2,3},{4,5,6,7},{9}};BooleanFound =false; for(intI=0;i<arr.length &&!found;i++)    {         for(intj=0;j<arr[i].length;j++) {System.out.println ("I= "+ i +", j= "+j); if(Arr[i][j] = = 5) {found=true;  Break; }        }    } 

How do I jump out of the current multiple nested loops in Java?

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.