Java jumps out of multiple loops

Source: Internet
Author: User

In Java, there are two ways to jump out of multiple loops

1. Set a tag before the loop statement, and then use the break statement with that tag to jump out of the loop

1  Public Static voidMain (String args[]) {2All: for(inti = 0; I < 100; i++) {3              for(intj = 1; J < I; J + +) {4                 if(i = = 10) {5                      BreakAll ;6                 }7             }8         }9}

When i=10, jump out of all loops

2. Set the identifier bit of a Boolean value in the For loop by determining whether to continue the loop to achieve the purpose

1  Public Static voidMain (String args[]) {2         intArr[][] = {{1, 2, 2}, {2, 2, 5}, {4, 4 } };3         BooleanFound =false;4          for(inti = 0; I < arr.length &&!found; i++) {5              for(intj = 0; J < Arr[i].length; J + +) {6System.out.println ("i=" + i + ", j=" +j);7                 if(Arr[i][j] = = 5) {8Found =true;9                      Break;Ten                 } One             } A         } -}

Java jumps out of multiple loops

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.