Java流程控制07:DoWhile循环

时间:2020-07-19 12:20:37   收藏:0   阅读:45

技术分享图片

while和do while的区别:

简单来说就是while如果不满足一次都不执行,do while 不满足至少执行一次

do while循环控制语句:

格式:

do{
   要执行的语句;    
}while(判断条件);
do while代码示例:
package com.wenjian.struct;

import java.sql.SQLOutput;

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);
    }
}

输出:
5050

进程已结束,退出代码 0
while 和 do while 区别代码示例:
package com.wenjian.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);
    }
}

输出:
=========
0

进程已结束,退出代码 0
markdown中ctrl+t,插入表格

原文:https://www.cnblogs.com/function123/p/13338786.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!