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

相关文章:

  • 2022-01-08
  • 2022-12-23
  • 2021-10-17
  • 2021-06-21
  • 2021-12-20
  • 2021-08-21
  • 2022-12-23
  • 2021-10-26
猜你喜欢
  • 2021-08-04
  • 2022-01-23
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
相关资源
相似解决方案