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循环


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM