Do while loop
From Seo Wiki - Search Engine Optimization and Programming Languages
| 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).
Contents |
[edit] 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;
[edit] Demonstrating do while loops
These example programs calculate the factorial of 5 using their respective languages' syntax for a do-while loop.
[edit] Ada
| File:Wikibooks-logo-en-noslogan.svg | The Wikibook Ada_Programming has a page on the topic of
Control </td>
</tr> </table> 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; [edit] C or C++unsigned int counter = 5; unsigned long factorial = 1; do { factorial *= counter--; /* Multiply, then decrement. */ } while (counter > 0); printf("%lu\n", factorial); [edit] JavaScriptvar counter = 5; var factorial = 1; do { factorial *= counter--; /* Multiply, then decrement. */ } while (counter > 0); document.body.appendChild(document.createTextNode(factorial)); [edit] FORTRAN 77Program 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 [edit] Pascalprogram Factorial; var Counter, Factorial: integer; begin Counter := 5; Factorial := 1; repeat Factorial := Factorial * Counter; Dec(Counter); until Counter = 0; WriteLn(Factorial) end. [edit] See alsode:Do-while-Schleife es:Bucle repetir eu:Repeat begizta hr:Do while petlja ja:Do-whileć–‡ |