java 中 break 只会跳出当前层的循环 不会跳出 上一次的循环
for(int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ if (j == 5){ break; } System.out.println("i ==" + i); System.out.println("j == " + j); } }
输出:
i ==0 j == 0 i ==0 j == 1 i ==0 j == 2 i ==0 j == 3 i ==0 j == 4 i ==1 j == 0 i ==1 j == 1 i ==1 j == 2 i ==1 j == 3 i ==1 j == 4 i ==2 j == 0 i ==2 j == 1 i ==2 j == 2 i ==2 j == 3 i ==2 j == 4 i ==3 j == 0 i ==3 j == 1 i ==3 j == 2 i ==3 j == 3 i ==3 j == 4 i ==4 j == 0 i ==4 j == 1 i ==4 j == 2 i ==4 j == 3 i ==4 j == 4 i ==5 j == 0 i ==5 j == 1 i ==5 j == 2 i ==5 j == 3 i ==5 j == 4 i ==6 j == 0 i ==6 j == 1 i ==6 j == 2 i ==6 j == 3 i ==6 j == 4 i ==7 j == 0 i ==7 j == 1 i ==7 j == 2 i ==7 j == 3 i ==7 j == 4 i ==8 j == 0 i ==8 j == 1 i ==8 j == 2 i ==8 j == 3 i ==8 j == 4 i ==9 j == 0 i ==9 j == 1 i ==9 j == 2 i ==9 j == 3 i ==9 j == 4
从上面可以看到,break 只是跳出当前层的循环