本來想測試一下。while里面用兩個if嵌套判斷的話,用break是跳出到什么地方。
代碼如下
1 public class testIFbreak { 2 public static void main(String[] args) { 3 int iLoopCount=0; 4 while (true) { 5 ++iLoopCount; 6 if (true) { 7 if (iLoopCount==2) { 8 System.out.println("內嵌的if\tiLoopCount==2"); 9 break; 10 } 11 System.out.println("外部的if"); 12 } 13 System.out.println("while里面"); 14 15 } 16 System.out.println("while外面"); 17 18 } 19 }
輸出的結果如下:
外部的if
while里面
內嵌的if iLoopCount==2
while外面
總結:
可以看出,當在內部的if里面用break之后,不是跳出if (iLoopCount==2)的判斷,然后執行System.out.println("外部的if");這句話。而是跳出整個while循環