Vòng lặp do while
Bản mẫu:Loop constructs
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);
Ada
with Ada.Integer_Text_IO;
procedure Factorial is
Counter : Integer:= 5;
Factorial: Integer:= 1;
begin
loop
Factorial:= Factorial * Counter;
Counter := Counter - 1;
exit when Counter = 0;
end loop;
Ada.Integer_Text_IO.Put (Factorial);
end 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 Integer
Dim counter As Integer
factorial = 1
counter = 5
Do
factorial = factorial * counter
counter = counter - 1
Loop While counter > 0
Print 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.[1]
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 *, factorial
end 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 = 5
var factorial = 1
do {
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 = 5
factorial = 1
while True:
factorial *= counter
counter -= 1
if counter == 0:
break
print(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 = 10
factorial = 2
begin
factorial *= counter
counter -= 2
end while counter > 1
puts 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 = 5
var factorial = 1
repeat {
factorial *= counter
counter -= 1
} while counter > 0
print(factorial)
Swift 1.x:
var counter = 5
var factorial = 1
do {
factorial *= counter
counter -= 1
} while counter > 0
println(factorial)
Visual Basic.NET
Dim counter As Integer = 5
Dim factorial As Integer = 1
Do
factorial *= counter
counter -= 1
Loop While counter > 0
Console.WriteLine(factorial)
Xem thêm
- Control flow
- For loop
- Foreach
- While loop
- Repeat loop (disambiguation)
Tham khảo
- ^ “Microsoft visual basic”. msdn.microsoft.com. Truy cập ngày 21 tháng 1 năm 2016.
- ^ “Mozilla Developer Network Documentation - The Do While Loop in JavaScript”.
- ^ “Kotlin Control Flow - While - Kotlin Programming Language”.