Vòng lặp do while

Trong hầu hết ngôn ngữ lập trình máy tính, một vòng lặp do while (tiếng Anh: do while loop) là một câu lệnh luồng điều khiển để thực thi một khối lệnh ít nhất một lần, và sau đó lặp lại việc thực thi khối đó, hay không, tùy thuộc vào điều kiện boolean ở cuối khối đó.

Cấu trúc tương đương

do {
   do_work();  
} while (condition);
thì tương đương với(==/===)
do_work();while (condition) {
   do_work();}
hay là
while (true) {
   do_work();
   if (!condition) break;}
hoặc
LOOPSTART:
   do_work();
   if (condition) goto LOOPSTART;

Vòng lặp do while ở các ngôn ngữ lập trình

Chương trình ví dụ sau tính toán giai thừa của 5 bằng cách dùng cú pháp của ngôn ngữ tương ứng cho một vòng lặpdo-while.

ActionScript 3

var counter:int = 5;var factorial:int = 1;do {
  factorial *= counter--; /* Multiply, then decrement. */} while (counter > 0);trace(factorial);

BASIC

BASIC trước kia (như GW-BASIC) dùng cú pháp WHILE/WEND.  Còn BASIC mới hơn như PowerBASIC cho phép dùng cả cấu trúc WHILE/WEND lẫn DO/LOOP, với cú pháp như DO WHILE/LOOP, DO UNTIL/LOOP, DO/LOOP WHILE, DO/LOOP UNTIL, và DO/LOOP. Mã nguồn BASIC điển hình như sau:
Dim factorial As IntegerDim counter As Integerfactorial = 1counter = 5Do 
   factorial = factorial * counter
   counter = counter - 1Loop While counter > 0Print factorial

C#

int counter = 5;int factorial = 1;do 
{
  factorial *= counter--; /* Multiply, then decrement. */} while (counter > 0);System.Console.WriteLine(factorial);

C

int counter = 5;int factorial = 1;do {
  factorial *= counter--; /* Multiply, then decrement. */} while (counter > 0);printf("factorial of 5 is %d\n", factorial);

C++

int counter = 5;int factorial = 1;do {
  factorial *= counter--;} while (counter > 0);std::cout<<"factorial of 5 is "<<factorial<<std::endl;

CFScript

factorial = 1;count = 10;do {
   factorial *= count--;} while (count > 1);writeOutput(factorial);

D

int counter = 5;int factorial = 1;do {
  factorial *= counter--; // Multiply, then decrement.} while (counter > 0);writeln("factorial of 5 is ", factorial);

Fortran

With legacy FORTRAN 77 there is no DO-WHILE construct but the same effect can be achieved with GOTO:
      INTEGER CNT,FACT
      CNT=5
      FACT=1
    1 CONTINUE      FACT=FACT*CNT
      CNT=CNT-1
      IF (CNT.GT.0) GOTO 1
      PRINT*,FACT
      END
With Fortran 90 and later, the do-while loop is actually the same as the for loop.
program FactorialProg
  integer:: counter = 5
  integer:: factorial = 1
  factorial = factorial * counter
  counter = counter - 1
  do while (counter > 0)
    factorial = factorial * counter
    counter = counter - 1
  end do  print *, factorialend program FactorialProg

Java

int counter = 5;int factorial = 1;do {
    factorial *= counter--; /* Multiply, then decrement. */} while (counter > 0);System.out.println("The factorial of 5 is " + factorial);

JavaScript

var counter = 5;var factorial = 1;do {
    factorial *= counter--;} while (counter > 0);console.log(factorial);

Kotlin

var counter = 5var factorial = 1do {
    factorial *= counter--}while(counter > 0)println("Factorial of 5 is $factorial")

PL/I

The PL/I DO statement subsumes the functions of the post-test loop (do until), the pre-test loop (do while), and the for loop. All functions can be included in a single statement. The example shows only the "do until" syntax.
declare counter   fixed initial(5);
declare factorial fixed initial(1);
do until(counter<=0);
  factorial = factorial * counter;
  counter = counter - 1;
  end;
put(factorial);

Python

Python lacks a specific do while flow control construct. However, the equivalent may be constructed out of a while loop with a break.
counter = 5factorial = 1while True:
    factorial *= counter
    counter -= 1
    if counter == 0:
        breakprint(factorial)

Racket

Racket, cũng như các hiện thực khác của Scheme, dùng "named-let" như một cách thông dụng để hiện thực vòng lặp:
#lang racket(define counter 5)(define factorial 1)(let loop ()
  (set! factorial (* factorial counter))
  (set! counter (sub1 counter))
  (when (> counter 0) (loop)))(displayln factorial)
Compare this with the first example of the while loop example for Racket. Be aware that a named let can also take arguments.
Racket and Scheme also provide a proper do loop.
(define (factorial n)
  (do ((counter n (- counter 1))
       (result 1 (* result counter)))
    ((= counter 0) result); Stop condition and return value.
   ; The body of the do-loop is empty.
   ))

Ruby

counter = 10factorial = 2begin
  factorial *= counter
  counter -= 2end while counter > 1puts factorial

Smalltalk

| counter factorial |counter:= 5.factorial:= 1.
[counter > 0] whileTrue: 
  [factorial:= factorial * counter.
  counter:= counter - 1].Transcript show: factorial printString

Swift

Swift 2.x:
var counter = 5var factorial = 1repeat {
     factorial *= counter
     counter -= 1} while counter > 0print(factorial)
Swift 1.x:
var counter = 5var factorial = 1do {
     factorial *= counter
     counter -= 1} while counter > 0println(factorial)

Visual Basic.NET

Dim counter As Integer = 5Dim factorial As Integer = 1Do
   factorial *= counter
   counter -= 1 
Loop While counter > 0Console.WriteLine(factorial)