zoukankan      html  css  js  c++  java
  • do-while循环结构

    do...while循环结构:

    • 对于while语句而言,如果不满足条件,则不能进入循环。但是有时候我们需要即使不满足条件,也至少执行一次。
    • do-while循环和while循环相似,不同的是:do-while循环至少会执行一次。

    语法:

    do{
       //代码语句
    }while(布尔表达式);

    while 和 do-while的区别:

    • while是先判断后执行,do-while是先执行后判断!
    • do-while总是保证循环体会被至少执行一次!这是他们主要的差别。

    使用 do-while计算100以内的和:

    package com.steven.demo;
    
    public class DoWhileDemo {
        public static void main(String[] args) {
            int i = 0;
            int sum = 0;
            do{
                sum = sum + i;
                i++;
            }while (i<=100);
            System.out.println(sum);
        }
    }

    while 和 do - while 循环的区别:

    package com.steven.demo;
    
    public class DoWhile2 {
        public static void main(String[] args) {
            int i = 1;
            int sum = 0;
    
            while (i < 1){
                sum = sum + i;
                i++;
            }
            System.out.println("while循环结构结果:" + sum);
    
            System.out.println("#########最美分割线###########");
    
            do {
                sum = sum + i;
                i++;
            }while (i < 1);
            System.out.println("do-while循环结构结果:" + sum);
        }
    }

    程序结果:

    通过程序结果我们就可以看出while循环和do-while循环的结果不一样。这是因为:while是先判断条件是否满足,然后再执行;而do-while是先执行,再判断条件是否满足。 

  • 相关阅读:
    文件转换table
    日期选择
    下拉多选
    下载文件(简洁方法)
    服务器下载文件http
    文件名称 (年4+月2+日2+时2+分2+秒2+毫秒3+8位随机数)
    Vim自动补全神器:YouCompleteMe
    Linux下如何卸载HP_LoadGenerator
    python 安装 setuptools Compression requires the (missing) zlib module 的解决方案
    linux安装IPython四种方法
  • 原文地址:https://www.cnblogs.com/stevenx/p/12984552.html
Copyright © 2011-2022 走看看