Do while loop

From Wikipedia, the free encyclopedia

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

Contents

[edit] Demonstrating do while loops

These do while loops will calculate the factorial of a number:

[edit] QBasic or Visual Basic

  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

[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] Java

  int counter = 5;
  long factorial = 1;
  do {
    factorial *= counter--; //Multiply, then decrement.
  } while (counter > 0);
  System.out.println(factorial);

[edit] REALbasic

  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 )

[edit] FORTRAN 77

  REAL MASS, MASLOS
  MASS = 7.0
  MASLOS = 1.0
  DO WHILE (MASS .GE. O.O)
    MASS = MASS - MASLOS
    WRITE(*,*),MASS
  END DO

[edit] Pascal

  program Factorial
  var
    Counter, Factorial: integer;
  begin
    Counter := 5;
    Factorial := 1;

    repeat
      Factorial := Factorial * Counter;
      Counter := Counter - 1;
    until ((Counter > 0)=false);

    Write(Factorial);
  end.
In other languages