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 @   陈哈哈  阅读(15665)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
点击右上角即可分享
微信分享提示