博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
do_Keyword
阅读量:3926 次
发布时间:2019-05-23

本文共 4151 字,大约阅读时间需要 13 分钟。

Java do while Loop with Examples

In this quick article, we will discuss how to use a do-while loop with examples. The do-while loop is similar to while loop, however, there is a difference between them: In a while loop, a condition is evaluated before the execution of loop’s body but in a do-while loop, a condition is evaluated after the execution of loop’s body.

The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.

Table of contents

  • do-while Loop Syntax
  • How do-while Loop Work?
  • Simple do-while Loop Example
  • do-while Loop with Menu Selection Example

1. do-while Loop Syntax

do {// body of a loop} while (condition);

Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of Java’s loops, a condition must be a Boolean expression.

2. How do-while Loop Works?

First, the statements inside loop execute and then the condition gets evaluated, if the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.

3. Simple do-while Loop Example

package net.javaguides.corejava.controlstatements.loops;public class DoWhileLoopExample {    public static void main(String args[]) {        int n = 10;        do {            System.out.println("tick " + n);            n--;        } while (n > 0);    }}

Output:

tick 10tick 9tick 8tick 7tick 6tick 5tick 4tick 3tick 2tick 1

4. do-while Loop with Menu Selection Example

The do-while loop is especially useful when you process a menu selection because you will usually want the body of a menu loop to execute at least once.

Consider the following program, which implements a very simple help system for Java’s selection and iteration statements:

package net.javaguides.corejava.controlstatements.loops;public class DoWhileMenuExample {    public static void main(String args[]) throws java.io.IOException {        char choice;        do {            System.out.println("Help on: ");            System.out.println(" 1. if");            System.out.println(" 2. switch");            System.out.println(" 3. while");            System.out.println(" 4. do-while");            System.out.println(" 5. for\n");            System.out.println("Choose one:");            choice = (char) System.in.read();        } while (choice < '1' || choice > '5');        System.out.println("\n");        switch (choice) {            case '1':                System.out.println("The if:\n");                System.out.println("if(condition) statement;");                System.out.println("else statement;");                break;            case '2':                System.out.println("The switch:\n");                System.out.println("switch(expression) {");                System.out.println(" case constant:");                System.out.println(" statement sequence");                System.out.println(" break;");                System.out.println(" //...");                System.out.println("}");                break;            case '3':                System.out.println("The while:\n");                System.out.println("while(condition) statement;");                break;            case '4':                System.out.println("The do-while:\n");                System.out.println("do {");                System.out.println(" statement;");                System.out.println("} while (condition);");                break;            case '5':                System.out.println("The for:\n");                System.out.print("for(init; condition; iteration)");                System.out.println(" statement;");                break;        }    }}

5. Here is a sample run produced by this program:

Help on:

    1. if
    1. switch
    1. while
    1. do-while
    1. for
      Choose one:
      4
      The do-while:
do {statement;} while (condition);

In the program, the do-while loop is used to verify that the user has entered a valid choice. If not, then the user is re-prompted. Since the menu must be displayed at least once, the do-while is the perfect loop to accomplish this.

转载地址:http://cbgrn.baihongyu.com/

你可能感兴趣的文章
ASP.NET Core中的分布式缓存
查看>>
在ASP.NET Core中创建自定义端点可视化图
查看>>
五年了,别再把务虚会开 “虚” 了
查看>>
快醒醒,C# 9 中又来了一堆关键词 init,record,with
查看>>
【招聘(深圳)】轻岁 诚聘.NET Core开发
查看>>
欢迎来到 C# 9.0(Welcome to C# 9.0)
查看>>
Dapr微服务应用开发系列1:环境配置
查看>>
使用 Visual Studio 2019 批量添加代码文件头
查看>>
【BCVP更新】StackExchange.Redis 的异步开发方式
查看>>
.NET5.0 Preview 8 开箱教程
查看>>
真・WPF 按钮拖动和调整大小
查看>>
做权限认证,还不了解IdentityServer4?不二话,赶紧拥抱吧,.NET Core官方推荐!...
查看>>
编写第一个 .NET 微服务
查看>>
深入探究.Net Core Configuration读取配置的优先级
查看>>
Blazor带我重玩前端(六)
查看>>
使用 C# 捕获进程输出
查看>>
数据库单表千万行 LIKE 搜索优化手记
查看>>
.NET Core 中生成验证码
查看>>
.NET Core 中导入导出Excel
查看>>
初识ABP vNext(8):ABP特征管理
查看>>