How do I break out of nested loops in Java?

前端 未结 30 2799
梦毁少年i
梦毁少年i 2020-11-21 11:51

I\'ve got a nested loop construct like this:

for (Type type : types) {
    for (Type t : types2) {
         if (some condition) {
             // Do somethin         


        
30条回答
  •  -上瘾入骨i
    2020-11-21 12:08

    Demo for break, continue, and label:

    Java keywords break and continue have a default value. It's the "nearest loop", and today, after a few years of using Java, I just got it!

    It's seem used rare, but useful.

    import org.junit.Test;
    
    /**
     * Created by cui on 17-5-4.
     */
    
    public class BranchLabel {
        @Test
        public void test() {
            System.out.println("testBreak");
            testBreak();
    
            System.out.println("testBreakLabel");
            testBreakLabel();
    
            System.out.println("testContinue");
            testContinue();
            System.out.println("testContinueLabel");
            testContinueLabel();
        }
    
        /**
         testBreak
         a=0,b=0
         a=0,b=1
         a=1,b=0
         a=1,b=1
         a=2,b=0
         a=2,b=1
         a=3,b=0
         a=3,b=1
         a=4,b=0
         a=4,b=1
         */
        public void testBreak() {
            for (int a = 0; a < 5; a++) {
                for (int b = 0; b < 5; b++) {
                    if (b == 2) {
                        break;
                    }
                    System.out.println("a=" + a + ",b=" + b);
                }
            }
        }
    
        /**
         testContinue
         a=0,b=0
         a=0,b=1
         a=0,b=3
         a=0,b=4
         a=1,b=0
         a=1,b=1
         a=1,b=3
         a=1,b=4
         a=2,b=0
         a=2,b=1
         a=2,b=3
         a=2,b=4
         a=3,b=0
         a=3,b=1
         a=3,b=3
         a=3,b=4
         a=4,b=0
         a=4,b=1
         a=4,b=3
         a=4,b=4
         */
        public void testContinue() {
            for (int a = 0; a < 5; a++) {
                for (int b = 0; b < 5; b++) {
                    if (b == 2) {
                        continue;
                    }
                    System.out.println("a=" + a + ",b=" + b);
                }
            }
        }
    
        /**
         testBreakLabel
         a=0,b=0,c=0
         a=0,b=0,c=1
         * */
        public void testBreakLabel() {
            anyName:
            for (int a = 0; a < 5; a++) {
                for (int b = 0; b < 5; b++) {
                    for (int c = 0; c < 5; c++) {
                        if (c == 2) {
                            break anyName;
                        }
                        System.out.println("a=" + a + ",b=" + b + ",c=" + c);
                    }
                }
            }
        }
    
        /**
         testContinueLabel
         a=0,b=0,c=0
         a=0,b=0,c=1
         a=1,b=0,c=0
         a=1,b=0,c=1
         a=2,b=0,c=0
         a=2,b=0,c=1
         a=3,b=0,c=0
         a=3,b=0,c=1
         a=4,b=0,c=0
         a=4,b=0,c=1
         */
        public void testContinueLabel() {
            anyName:
            for (int a = 0; a < 5; a++) {
                for (int b = 0; b < 5; b++) {
                    for (int c = 0; c < 5; c++) {
                        if (c == 2) {
                            continue anyName;
                        }
                        System.out.println("a=" + a + ",b=" + b + ",c=" + c);
                    }
                }
            }
        }
    }
    

提交回复
热议问题