- 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少要执行一次。
- do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。
- 语法格式
复制代码
1
2
3
4do{ //代码语句 }while(布尔表达式)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package com.Feng.struct; public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; }while (i<=100); System.out.println(sum); System.out.println(i); } }
- while和do-while的区别:
while先判断后执行。do-while是先执行后判断!
do…while总是保证循环体会被至少执行一次,这是他们的主要差别。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package com.Feng.struct; public class DoWhileDemo02 { public static void main(String[] args) { int a = 0; while (a<0){ System.out.println(a); a++; } System.out.println("-------------------"); do { System.out.println(a); a++; }while (a<0); } }
最后
以上就是缥缈发箍最近收集整理的关于Java流程控制——do-while循环的全部内容,更多相关Java流程控制——do-while循环内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复