Do while loop

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Do While loop diagram

In most computer programming languages, a do while loop, sometimes just called a while loop, is a control flow statement that allows code to be executed once based on a given Boolean condition. Note though that unlike most languages, Fortran's do loop is actually the same as the for loop.

The do while construct consists of a process symbol 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.The do-while loop is an exit-condition loop. This means that the code must always be executed first and then the expression or test condition is evaluated. If it is true, the code executes the body of the loop again. This process is repeated as long as the expression evaluates to true. if the expression is false, the lop terminates and control transfers to the statement following the do-while loop.

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 "while" loop runs while the control expression is true (and terminates once the expression becomes false). Bash did not support this control flow statement.

Contents

Equivalent constructs [edit]

do {
   do_work();
} while (condition);

is equivalent to

do_work();
while (condition) {
   do_work();
}

that (as long as the continue statement is not used) is technically equivalent to the following (though these examples are not typical or modern style):

while (true) {
   do_work();
   if (!condition) break;
}

or

LOOPSTART:
   do_work();
   if (condition) goto LOOPSTART;

Demonstrating do while loops [edit]

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

ActionScript 3 [edit]

var counter:int = 5;
var factorial:int = 1;
do {
  factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
trace(factorial);

Ada [edit]

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 Factorial;

BASIC [edit]

dim f as integer
do while  f<10
    f=f+1
    msgbox (f)
loop

C# [edit]

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

C [edit]

int counter = 5;
int factorial = 1;
do {
  factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
printf("factorial of 5 is %d\n", factorial);

C++ [edit]

int counter = 5;
int factorial = 1;
do {
  factorial *= counter--;
} while (counter > 0);
cout<<"factorial of 5 is "<<factorial<<endl;

Fortran [edit]

program FactorialProg
  integer :: counter = 5
  integer :: factorial = 1
  do
    factorial = factorial * counter
    counter = counter - 1
    if (counter == 0) exit
  end do
  print *, factorial
end program FactorialProg

With Fortran 90 and later, we use the equivalent construct mentioned above. Using it is better practice, since it is immediately apparent that this is a while-loop.

program FactorialProg
  integer :: counter = 5
  integer :: factorial = 1
  factorial = factorial * counter
  counter = counter - 1
  do while (counter > 0)
    factorial = factorial * counter
    counter = counter - 1
  end do
  print *, factorial
end program FactorialProg

Java [edit]

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

JavaScript [edit]

var counter = 5;
var factorial = 1;
do {
    factorial *= counter--;
} while (counter > 0);
console.log(factorial);

[1]

Perl [edit]

$counter = 5;
$factorial = 1;
do {
     $factorial *= $counter--; # Multiply, then decrement
} while ($counter > 0);
print $factorial;

PHP [edit]

<?php
$counter = 5;
$factorial = 1;
do {
     $factorial *= $counter--;
} while ($counter > 0);
echo $factorial;
?>

[2]

PL/I [edit]

The PL/I DO statement subsumes the functions of the post-test loop (do until), the pre-test loop (do while), and the for loop. All functions can be included in a single statement. The example shows only the "do until" syntax.

declare counter   fixed initial(5);
declare factorial fixed initial(1);
do until(counter<=0);
  factorial = factorial * counter;
  counter = counter - 1;
  end;
put(factorial);

Racket [edit]

In Racket, as in other Scheme implementations, a "named-let" is a popular way to implement loops:

#lang racket
(define counter 5)
(define factorial 1)
(let loop ()
  (set! factorial (* factorial counter))
  (set! counter (sub1 counter))
  (when (> counter 0) (loop)))
(displayln factorial)

Compare this with the first example of the while loop example for Racket.

Ruby [edit]

counter = 5
factorial = 1
begin
  factorial *= counter
  counter -= 1
end while counter > 0
puts factorial

Smalltalk [edit]

| counter factorial |
counter := 5.
factorial := 1.
[counter > 0] whileTrue: 
  [factorial := factorial * counter.
  counter := counter - 1].
Transcript show: factorial printString

Visual Basic.Net [edit]

Dim counter As Integer = 5
Dim factorial As Integer = 1
Do
   factorial *= counter
   counter -= 1 
Loop While counter > 0
Console.WriteLine(factorial)

See also [edit]

References [edit]

  1. ^ "Mozilla Developer Network Documentation - The Do While Loop in JavaScript". 
  2. ^ "Do While - Official PHP Documentation". 

http://www.byte-notes.com/do-while-loop-c