Do While Loop
Do While Loop
do {
do_work();
} while (condition);
is equivalent to(==/===)
do_work();
while (condition) {
do_work();
}
In this manner, the do ... while loop saves the initial "loop priming" with do_work(); on the line
before the while loop.
As long as the continue statement is used, the above is technically equivalent to the following
(though these examples are not typical or modern style used in everyday computers):
while (true) {
do_work();
if (!condition) break;
}
or
LOOPSTART:
do_work();
if (condition) goto LOOPSTART;
ActionScript 3[edit]
var counter:int = 5;
var factorial:int = 1;
do {
factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
trace(factorial);
Ada[edit]
The
Wikibook Ada_Programming
has a page on the topic
of: 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[edit]
Early BASICs (such as GW-BASIC) used the syntax WHILE/WEND. Modern BASICs such
as PowerBASIC provide both WHILE/WEND and DO/LOOP structures, with syntax such as DO
WHILE/LOOP, DO UNTIL/LOOP, DO/LOOP WHILE, DO/LOOP UNTIL, and DO/LOOP (without
outer testing, but with a conditional EXIT LOOP somewhere inside the loop). Typical BASIC source
code:
C#[edit]
int counter = 5;
int factorial = 1;
do
{
factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
System.Console.WriteLine(factorial);
C[edit]
int counter = 5;
int factorial = 1;
do {
factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
printf("factorial of 5 is %d\n", factorial);
C++[edit]
int counter = 5;
int factorial = 1;
do {
factorial *= counter--;
} while (counter > 0);
std::cout<<"factorial of 5 is "<<factorial<<std::endl;
CFScript[edit]
factorial = 1;
count = 10;
do {
factorial *= count--;
} while (count > 1);
writeOutput(factorial);
D[edit]
int counter = 5;
int factorial = 1;
do {
factorial *= counter--; // Multiply, then decrement.
} while (counter > 0);
writeln("factorial of 5 is ", factorial);
Fortran[edit]
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[edit]
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[edit]
var counter = 5;
var factorial = 1;
do {
factorial *= counter--;
} while (counter > 0);
console.log(factorial);
[2]
Kotlin[edit]
var counter = 5
var factorial = 1
do {
factorial *= counter--
}while(counter > 0)
println("Factorial of 5 is $factorial")
[3]
PL/I[edit]
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.
Python[edit]
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[edit]
In Racket, as in other Scheme implementations, a "named-let" is a popular way to implement loops:
#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[edit]
counter = 10
factorial = 2
begin
factorial *= counter
counter -= 2
end while counter > 1
puts factorial
Smalltalk[edit]
| counter factorial |
counter := 5.
factorial := 1.
[counter > 0] whileTrue:
[factorial := factorial * counter.
counter := counter - 1].
Transcript show: factorial printString
Swift[edit]
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)