📑 Table of Contents

do-while循环(英語:do while loop),也有稱do循环,是電腦程式語言中的一種控制流程語句。主要由一個代碼塊(作為迴圈)和一個表達式(作為迴圈條件)組成,表達式為布林(boolean)型。迴圈內的代碼執行一次後,程序會去判斷這個表達式的返回值,如果這個表達式的返回值為“true”(即滿足迴圈條件)時,則迴圈內的代碼會反覆執行,直到表達式的返回值為“false”(即不滿足迴圈條件)時終止。程序會在每次迴圈執行一次後,進行一次表達式的判斷。

do-while循环执行过程

一般情況下,do-while迴圈與while循环相似。兩者唯一的分別:do-while迴圈將先會執行一次迴圈內的代碼,再去判斷迴圈條件。所以無論迴圈條件是否滿足,do-while迴圈內的代碼至少會執行一次。因此,do-while迴圈屬於後測循環(post-test loop)。

一些語言有其他的表達方式。例如Pascal就提供 repeat until 循环,運作方法剛剛相反。 repeat 部分不斷重複,直到 until 條件滿足。換言之, until 條件是 false 的時候,迴圈會繼續執行。

程序示例

编辑

C

编辑
#include <stdio.h>
#include <stdlib.h>
int main()
{  
    int i = 5; /*宣告整數i*/
    do {
        i--;
    } while (i > 0);
    printf("%d",i);
    return 0;
}

C#

编辑
using System;
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 5; /*宣告整數i*/
            do {
                i--;
            } while (i > 0);
            Console.WriteLine(i);
        }
    }
}

Java

编辑
class main {
    public static void main (String args[]){
    int i = 5;
    do {
        i--; // 迴圈
    } while (i > 0); // 迴圈條件
    System.out.println(i);
    }
}


📚 Artikel Terkait di Wikipedia

伊朗戰爭 (2026年)

例如为了在以色列如期发动袭击后,先发制人地防止伊朗随后对涉及美国的资产进行报复; ;抵御伊朗迫在眉睫的威胁;摧毁伊朗的导弹计划(英语:Ballistic_missile_program_of_Iran)和军事能力;防止伊朗获取核武器; 确保获得伊朗的油气资源(英语:Oil and gas reserves and resource

结构化编程

以下是是一個由檔案中讀取資料並處理的程式範例: open file; while (reading not finished) { read some data; if (error) { stop the subprogram and inform rest of the program about the error; } }

续体

applicable to the value of an expression while returning an "answer"; i.e. the prophesized result of the whole program in which the expression is embedded

杰克逊结构化编程

有一個輸入指令。傑克森在《Principles of Program Design》的第三章,列出了二個程式以說明不同結構化程式設計的差異。 此程式的JSP版本如下 String line; line = in.readLine(); while (line != null) { int count

坎巴拉太空计划2

with Kerbal Space Program 2. GamesIndustry.biz. 2019-10-28 [2020-06-06]. (原始内容存档于2019-12-10).  Kerbal Space Program 2 heads to space while grounded in science

达夫设备

在计算机科学领域,达夫设备(英文:Duff's device)是串行复制(serial copy)的一种优化(英语:Program optimization)实现,通过汇编语言编程时一常用方法,实现展开循环,进而提高执行效率。这一方法据信为当时供职于卢卡斯影业的汤姆·达夫(英语:Tom

归并排序

while (start1 < end1 && start2 < end2) b[k++] = a[start1] < a[start2] ? a[start1++] : a[start2++]; while (start1 < end1) b[k++] = a[start1++]; while (start2

GnuCOBOL

GnuCOBOL (曾称OpenCOBOL 、GNU Cobol )是COBOL程式语言的自由实现,最初由Keisuke Nishida设计,由Roger While负责領導開發,最近的版本是由Simon Sobisch,Sergey Kashyrin,Ron Norman,Edward Hart和其他许多人领导。