Do while loop

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

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

<source lang="c"> do {

  statements;

} while (condition); </source>

is not equivalent to

<source lang="c"> statements; while (condition) {

  statements;

} </source>

but (as long as the continue statement is not used) is equivalent to

<source lang="c"> while (true) {

  statements;
  if (!condition) break;

} </source>

or to

<source lang="c"> LOOPSTART:

  statements;
  if (condition) goto LOOPSTART;

</source>

Demonstrating do while loops

These example programs calculate the factorial of 5 using their respective languages' syntax for a do-while loop.


Ada

<source lang="ada"> 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; </source>

C or C++

<source lang="c"> unsigned int counter = 5; unsigned long factorial = 1; do {

 factorial *= counter--; /* Multiply, then decrement. */

} while (counter > 0); printf("%lu\n", factorial); </source>

JavaScript

<source lang="javascript"> var counter = 5; var factorial = 1; do {

 factorial *= counter--; /* Multiply, then decrement. */

} while (counter > 0); document.body.appendChild(document.createTextNode(factorial)); </source>

FORTRAN 77

<source lang="fortran">

     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

</source>

Pascal

<source lang="pascal"> program Factorial; var

 Counter, Factorial: integer;

begin

 Counter := 5;
 Factorial := 1;
 repeat
   Factorial := Factorial * Counter;
   Dec(Counter);
 until Counter = 0;
 WriteLn(Factorial)

end. </source>

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...