Do while loop
This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (December 2009) |
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 has a "repeat until" loop, which continues to run until the control expression is true (and then terminates) — whereas a "do-while" loop runs while the control expression is true (and terminates once the expression becomes false).
Equivalent constructs
do {
statements;
} while (condition);
is not equivalent to
statements;
while (condition) {
statements;
}
but (as long as the continue statement is not used) is equivalent to
while (true) {
statements;
if (!condition) break;
}
or to
LOOPSTART:
statements;
if (condition) goto LOOPSTART;
Demonstrating do while loops
These example programs calculate the factorial of 5 using their respective languages' syntax for a do-while loop.
Ada
Search Wikibooks | The Wikibook Ada_Programming has a page on the topic of |
with Ada.Integer_Text_IO;
procedure Factorial is
Counter : Integer := 10;
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;
C or C++
unsigned int counter = 5;
unsigned long factorial = 1;
do {
factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
printf("%lu\n", factorial);
JavaScript
var counter = 5;
var factorial = 1;
do {
factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
document.body.appendChild(document.createTextNode(factorial));
FORTRAN 77
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
Pascal
program Factorial;
var
Counter, Factorial: integer;
begin
Counter := 5;
Factorial := 1;
repeat
Factorial := Factorial * Counter;
Dec(Counter);
until Counter = 0;
WriteLn(Factorial)
end.
See also
cs:Cyklus do-while de:Do-while-Schleife es:Bucle repetir eu:Repeat begizta hr:Do while petlja ja:Do-while文
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...
- Pages using deprecated source tags
- Pages with syntax highlighting errors
- Pages with broken file links
- Articles lacking sources from December 2009
- Articles with invalid date parameter in template
- All articles lacking sources
- Control flow
- Articles with example Ada code
- Articles with example C code
- Articles with example Fortran code
- Articles with example Pascal code