In most computer programming languages, a do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Note though that unlike most languages, Fortran's do loop is actually analogous to the for loop.
The do while construct consists of a block of code and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed.
It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that allows termination of the loop.
Some languages may use a different naming convention for this type of loop. For example, the Pascal language refers to it as a "repeat until" loop. Note that in Pascal, the loop will continue to run until the control expression is "true" as opposed to other languages where the loop terminates when the control expression is "false".
For example, in the C programming language, the code fragment:
x = 0;
do {
x = x + 1;
} while (x < 3);
first executes the instruction x = x + 1, which results in x = 1. It then checks the condition x < 3, which is true, so it executes the code block again. It repeats this execute and check process until the variable x has the desired value, 3.
Equivalent Constructs
do {
statements;
} while (condition);
is equivalent to
while (true) {
statements;
if (!condition) break;
}
or
LOOPSTART:
statements;
if (condition) goto LOOPSTART;
Demonstrating do while loops
These do while loops will calculate the factorial of a number:
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 Main;
var counter:Number = 5;
var factorial:Number = 1;
do {
factorial *= counter;
counter --;
} while (counter > 0)
trace(factorial);
ALGOL 68 is unusual as typically the "DO ~ WHILE ~" is an adaptation of the "WHILE ~ DO ~ OD" loop clause.
PRAGMAT precision 101 PRAGMAT
INT counter:= 70;
LONG LONG INT factorial := 1;
WHILE # Multiply, then decrement.#
factorial *:= counter;
counter-:=1;
counter > 0 # the result becomes the test condition #
DO SKIP OD;
printf(($" one googol:"101dl$, factorial))
Output:
one googol:11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000
Dim counter As Integer : counter = 5 Dim factorial as Long : factorial = 1 Do
factorial = factorial * counter 'multiply
counter = counter - 1 'decrement
Loop While (counter > 0) Print factorial
end sub
Dim counter As UInteger = 5
Dim factorial as ULong = 1
Do
factorial *= counter 'multiply
counter -= 1 'decrement
Loop While counter > 0
System.Console.WriteLine(factorial)
unsigned int counter = 5;
unsigned long factorial = 1;
do {
factorial *= counter--; /*Multiply, then decrement.*/
} while (counter > 0);
printf("%lu\n", factorial);
uint counter = 5;
ulong factorial = 1;
do
{
factorial *= counter--;
}
while (counter > 0);
System.Console.WriteLine(factorial);
uint i = 0;
do {
++i;
writefln(i);
} while(i < 5);
int counter = 5;
long factorial = 1;
do {
factorial *= counter--; //Multiply, then decrement.
} while (counter > 9);
System.out.println(factorial);
Dim counter As Integer = 5
Dim factorial as Integer = 1
Do
factorial = factorial * counter // multiply
counter = counter - 1 // decrement
While counter > 0
MsgBox Str( factorial )
Loop
Program FACTORIALPROG
Integer start
Integer factorial,counter
Data Start /5/
Factorial = 1
Counter = Start
do while(counter>0)
Factorial = Factorial*counter
Counter = Counter-1
end do
print *,Factorial
End Program
program Factorial;
var
Counter, Factorial: integer;
begin
Counter := 5;
Factorial := 1;
repeat
Factorial := Factorial * Counter;
Counter := Counter - 1;
until Counter = 0;
Write(Factorial);
end.
See also
|