In java, how does one jump out of the current multi-nested loop?

Source: Internet
Author: User

1. How to jump out of the current multi-nested loop in java? In java, if you want to jump out of multiple nested loops, you can define a label before the loop statement, and then use the break statement with the label in the code in the loop body to jump out of the outer loop. For example, [java] public static void main (String [] args) {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 ;}} actually does not recommend this method, it is recommended that the results of the outer loop expression be controlled by the code of the loop body in the lower layer. For example: [java] www.2cto. compublic static void main (String [] args) {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 ;}}}}

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.