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