Java 中do...while()的使用

do…while语句

基本格式:

do {
循环体语句
}while(条件判断语句);

完整格式:

初始化语句;
do{
循环体语句
条件控制语句
}while(条件判断语句);

执行流程:

  1. 执行初始化语句

  2. 执行循环语句

  3. 执行条件控制语句

  4. 执行条件判断语句,看其结果是true还是false
    如果为true,继续执行
    如果为false,循环结束

  5. 回到2继续执行

public class doWhileTest {
    
    
    public static void main(String[] args) {
    
    
        //需求:在控制台输出5次hello,world
        for (int i = 1; i <= 5; i++) {
    
    
            System.out.println("hello,world");
        }
        System.out.println("---------");
        int j = 1;
        do {
    
    
            System.out.println("hello,world");
            j++;
        } while (j <= 5);
    }
}

输出结果:

hello,world
hello,world
hello,world
hello,world
hello,world
---------
hello,world
hello,world
hello,world
hello,world
hello,world

猜你喜欢

转载自blog.csdn.net/lu202032/article/details/122510994