While loop

From Free net encyclopedia

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.

The while construct consists of a block of code and a condition. The condition is first evaluated - if the condition is true the code within the block is then executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.

Note that 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 controls termination of the loop.

For example, in the C programming language, the code fragment

x = 0;
while (x < 3)
{
   x++;
}

first checks whether x is larger than 3, which it is not, so it increments x by 1. It then checks the condition again, and executes again, repeating this process until the variable x has the desired value, 3.

Contents

Demonstrating while loops

These while loops will calculate the factorial of a number:

QBasic or Visual Basic

 Dim counter as Integer : counter = 5
 Dim factorial as Long : factorial = 1
 While (counter > 0)
   factorial = factorial * counter     'Multiply
   counter = counter - 1               'Decrement
 Wend
 Print factorial                       'Prints out the result.


REALbasic

 Dim counter as Integer = 5
 Dim factorial as Integer = 1
 While counter > 0
   factorial = factorial * counter     //Multiply
   counter = counter - 1               //Decrement
 Wend
 MsgBox Str( factorial )               // Prints out the result.

C or [[C++]]

 unsigned int counter = 5;
 unsigned long factorial = 1;
 while (counter > 0)
{
   factorial *= counter--; /*Multiply, then decrement.*/
 printf("%i", factorial);
 scanf( counter &factorial);
}

Perl

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

Very similar to C and C++, but the while loop could also have been written on one line:

 $factorial *= $counter-- while ( $counter > 0 );

Tcl (Tool command language)

 set counter 5
 set factorial 1
 while {$counter > 0} {
   set factorial [expr $factorial * $counter] 
   incr counter -1 
 }
 puts $factorial

Java, Csharp

The code for the loop is the same for Java and Csharp:

 int counter = 5;
 long factorial = 1;
 while (counter > 0)
    factorial *= counter--; // Multiply, then decrement.

For Java the result is printed as follows:

 System.out.println(factorial);

The same in C#

 System.Console.WriteLine(factorial);

Pascal

 program Factorial
 var
   Counter, Factorial: integer;
 begin
   Counter := 5;
   Factorial := 1;
   while Counter > 0 do begin
     Factorial := Factorial * Counter;
     Counter := Counter - 1;
   end;
   Write(Factorial);
 end.

Python

 counter = 5
 factorial = 1
 while counter>0:
       factorial *= counter
       counter -= 1
 print factorial

See also

ja:While文