Jump out of the inner for loop without executing the outer for loop remainder statement

Source: Internet
Author: User

When you encounter the need to jump out of the inner for loop in your project, do not execute the outer for loop remaining statements and start the next outer for loop issue directly.

I personally do not want to use variables to determine whether continue, and finally found that the problem can be solved by naming the for loop and then continue to a specific for loop.

So write an example below to record.


Gets the odd number that the second array in the first array does not have, the following code:

Int[] one = {1, 4, 2, 8, 5, 7};
Int[] Two = {3, 0, 5, 6, 8, 9};

w:for (int i = 0; i < one.length i++) {
    if (One[i]% 2 = 1) {for
        (int j = 0; J < Two.length; J +) {
            I F (one[i] = = Two[j]) {
                continue w;
            }
        }
        System.out.println (One[i]);
    }

This allows you to continue to the specified for loop, skipping the remaining statements in the outer loop.

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.