While loop
From Wikipedia, the free 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 loop can be thought of as a repeating if statement.
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.
For example, in the C programming language (as well as Java and C++, which use the same syntax in this case), the code fragment
x = 0; while (x < 3) { x++; }
first checks whether x is lesser than 3, which it is, so it increments x by 1. It then checks the condition again, and executes again, repeating this process until the variable x has the value 3.
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.
Contents |
[edit] Equivalent constructs
while (condition) { statements; }
is equivalent to
while (true) { if (!condition) break; statements; }
or
goto TEST; LOOPSTART: statements; TEST: if (condition) goto LOOPSTART;
Also, in C and its descendants, a while loop is a for loop with no initialization or counting expressions, i.e.,
for ( ; condition; ) { statements; }
[edit] Demonstrating while loops
These while loops will calculate the factorial of the number 5:
[edit] Ada
with Ada.Integer_Text_IO; procedure Factorial is Counter : Integer := 5; Factorial : Integer := 1; begin while Counter > 0 loop Factorial := Factorial * Counter; Counter := Counter - 1; end loop; Ada.Integer_Text_IO.Put (Factorial); end Main;
[edit] AutoIt
Dim $counter = 5, $factorial = 1 While $counter > 0 $factorial *= $counter $counter -= 1 WEnd MsgBox(0,"Factorial", $factorial)
[edit] QBasic or Visual Basic
' Initialize the variables Dim counter As Integer : counter = 5 Dim factorial As Long : factorial = 1 Do While counter > 0 factorial = factorial * counter ' Multiply counter = counter - 1 ' Decrement Loop Print factorial ' Prints out the result.
[edit] Visual Basic .NET
' Initialize the variables Dim counter As UInteger = 5 Dim factorial As UInteger = 1 Do While counter > 0 factorial = factorial * counter ' Multiply counter -= 1 ' Decrement Loop System.Console.WriteLine(factorial) ' Prints out the result.
[edit] C or C++
unsigned int counter = 5; unsigned long factorial = 1; while (counter > 0) { factorial *= counter--; //Multiply and decrement } printf("%i", factorial);
[edit] Java, C#
The code for the loop is the same for Java and C#:
int counter = 5; long factorial = counter; while (--counter > 1) { factorial *= counter; }
For Java the result is printed as follows:
System.out.println(factorial);
The same in C#
System.Console.WriteLine(factorial);
[edit] JavaScript
var counter = 5; var factorial = counter; while (--counter > 1) { factorial *= counter; } document.body.appendChild(document.createTextNode(factorial));
[edit] Matlab
counter = 5; factorial = 1; while (counter > 0) factorial = factorial * counter; %Multiply counter = counter - 1; %Decrement end factorial
[edit] 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.
[edit] 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;
While loops are frequently used for reading data line by line (as defined by the $/ line separator) from open filehandles:
open IN, "<test.txt"; while ( <IN> ) { print; } close IN;
[edit] PHP
$counter = 5; $factorial = 1; while($counter > 0) { $factorial *= $counter--; // Multiply, then decrement. } echo $factorial;
[edit] Python
counter = 5 factorial = 1 while counter > 0: factorial *= counter counter -= 1 print factorial
[edit] Smalltalk
Contrary to other languages, in Smalltalk a while loop is not a language construct but defined in the class BlockClosure as a method with one parameter, the body as a closure, using self as the condition.
| count factorial | count := 5. factorial := 1. [ count > 0 ] whileTrue: [ factorial := factorial * (count := count - 1) ] Transcript show: factorial
[edit] Tcl (Tool command language)
set counter 5 set factorial 1 while {$counter > 0} { set factorial [expr $factorial * $counter] incr counter -1 } puts $factorial
[edit] Windows PowerShell
$counter = 5
$factorial = 1
while ($counter -gt 0) {
$factorial *= $counter-- # Multiply, then decrement.
}
Write-Output $factorial

