基本语法
do…while循环和while循环非常类似,只不过do…while循环至少会执行一次循环体。
do…whie的语法如下:
do {
//循环体
} while(布尔表达式);
需要注意的是,布尔表达式出现在循环的最后面,因此在判断布尔表达式时,循环体至少已经被执行过一次。当布尔表达式为true时,程序跳转到循环体中去执行,循环体中的语句也就被再一次执行了。这样的过程持续到布尔表达式的值为false。
流程图
使用示例
public class Test {
public static void main(String args[]) {
int x = 10;
do {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
程序运行结果输出如下:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
关注公众号「小白轻松学编程」
更多交流,欢迎微信搜索并关注公众号「小白轻松学编程」!
博客里所有教程会第一时间在公众号上更新哟,扫码关注一下吧~