java 跳出 if

本来想测试一下。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循环

posted @ 2012-09-06 23:30  陈哈哈  阅读(15648)  评论(0编辑  收藏  举报