Vòng Lặp Do While – Wikipedia Tiếng Việt

Bài viết này là một bài mồ côi vì không có bài viết khác liên kết đến nó. Vui lòng tạo liên kết đến bài này từ các bài viết liên quan; có thể thử dùng công cụ tìm liên kết. (tháng 7 2020)
Sơ đồ luồng 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

[sửa | sửa mã nguồn] 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)gotoLOOPSTART;

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

[sửa | sửa mã nguồn]

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

[sửa | sửa mã nguồn] varcounter:int=5; varfactorial:int=1; do{ factorial*=counter--;/* Multiply, then decrement. */ }while(counter>0); trace(factorial);

Ada

[sửa | sửa mã nguồn] Wikibook Ada_Programming có một trang Control 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

[sửa | sửa mã nguồn]

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:

DimfactorialAsInteger DimcounterAsInteger factorial=1 counter=5 Do factorial=factorial*counter counter=counter-1 LoopWhilecounter>0 Printfactorial

C#

[sửa | sửa mã nguồn] intcounter=5; intfactorial=1; do { factorial*=counter--;/* Multiply, then decrement. */ }while(counter>0); System.Console.WriteLine(factorial);

C

[sửa | sửa mã nguồn] intcounter=5; intfactorial=1; do{ factorial*=counter--;/* Multiply, then decrement. */ }while(counter>0); printf("factorial of 5 is %d\n",factorial);

C++

[sửa | sửa mã nguồn] intcounter=5; intfactorial=1; do{ factorial*=counter--; }while(counter>0); std::cout<<"factorial of 5 is "<<factorial<<std::endl;

CFScript

[sửa | sửa mã nguồn] factorial=1; count=10; do{ factorial*=count--; }while(count>1); writeOutput(factorial);

D

[sửa | sửa mã nguồn] intcounter=5; intfactorial=1; do{ factorial*=counter--;// Multiply, then decrement. }while(counter>0); writeln("factorial of 5 is ",factorial);

Fortran

[sửa | sửa mã nguồn]

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 1CONTINUE FACT=FACT*CNT CNT=CNT-1 IF(CNT.GT.0)GOTO1 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

[sửa | sửa mã nguồn] intcounter=5; intfactorial=1; do{ factorial*=counter--;/* Multiply, then decrement. */ }while(counter>0); System.out.println("The factorial of 5 is "+factorial);

JavaScript

[sửa | sửa mã nguồn] varcounter=5; varfactorial=1; do{ factorial*=counter--; }while(counter>0); console.log(factorial);

[2]

Kotlin

[sửa | sửa mã nguồn] varcounter=5 varfactorial=1 do{ factorial*=counter-- }while(counter>0) println("Factorial of 5 is $factorial")

[3]

PL/I

[sửa | sửa mã nguồn]

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

[sửa | sửa mã nguồn]

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

[sửa | sửa mã nguồn]

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 (definecounter5) (definefactorial1) (letloop() (set!factorial(*factorialcounter)) (set!counter(sub1counter)) (when(>counter0)(loop))) (displaylnfactorial)

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(factorialn) (do((countern(-counter1)) (result1(*resultcounter))) ((=counter0)result); Stop condition and return value. ; The body of the do-loop is empty. ))

Ruby

[sửa | sửa mã nguồn] counter=10 factorial=2 begin factorial*=counter counter-=2 endwhilecounter>1 putsfactorial

Smalltalk

[sửa | sửa mã nguồn] | counter factorial | counter:= 5. factorial:= 1. [counter > 0] whileTrue: [factorial:= factorial * counter. counter:= counter - 1]. Transcript show: factorial printString

Swift

[sửa | sửa mã nguồn]

Swift 2.x:

varcounter=5 varfactorial=1 repeat{ factorial*=counter counter-=1 }whilecounter>0 print(factorial)

Swift 1.x:

varcounter=5 varfactorial=1 do{ factorial*=counter counter-=1 }whilecounter>0 println(factorial)

Visual Basic.NET

[sửa | sửa mã nguồn] DimcounterAsInteger=5 DimfactorialAsInteger=1 Do factorial*=counter counter-=1 LoopWhilecounter>0 Console.WriteLine(factorial)

Xem thêm

[sửa | sửa mã nguồn]
  • Control flow
  • For loop
  • Foreach
  • While loop
  • Repeat loop (disambiguation)

Tham khảo

[sửa | sửa mã nguồn]
  1. ^ "Microsoft visual basic". msdn.microsoft.com. Truy cập ngày 21 tháng 1 năm 2016.
  2. ^ "Mozilla Developer Network Documentation - The Do While Loop in JavaScript".
  3. ^ "Kotlin Control Flow - While - Kotlin Programming Language".

Liên kết ngoài

[sửa | sửa mã nguồn]
  • do {...} while (0) in C macros

Từ khóa » Câu Lệnh While