网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 生活知识 知识问答

while 和 do while 区别

时间:2026-02-14 09:17:32

1、创建一个类.CLASS文件,建一个main方法。

写入下面代码体验一下两者的区别。

  boolean condition =false;

 int i=0;

 System.out.println("i的初始值为"+i);

while(condition){

i++;

 

}

System.out.println("经过while循环之后,"+i);

do{

i++;  

}while(condition);

System.out.println("经过了do_while循环后"+i+"i加1");

 }

具体参考下图。

while 和 do while 区别

2、点击运行,看控制台结果。我们发现do while i的值为1。

while 和 do while 区别

3、如果condition为true呢?

© 2026 智德知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com