Java do…while循环

  • Post author:
  • Post category:java


在这里插入图片描述



dowhile示例

package com.crd.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do {              //先执行再判断
            sum=sum+i;
            i++;
        }while(i<=100);
    }
}



用程序表示while 和do while 的区别

package com.crd.struct;

import java.util.Arrays;

public class DoWhilieDemo02 {
    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);


    }
}



运行结果

在这里插入图片描述


可以看出,while是先判断再执行,dowhile是先执行再判断



版权声明:本文为qq_46759852原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。