<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=While_loop</id>
	<title>While loop - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=While_loop"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=While_loop&amp;action=history"/>
	<updated>2026-06-21T04:25:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=While_loop&amp;diff=289841&amp;oldid=prev</id>
		<title>imported&gt;FaviFake: update merge notice</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=While_loop&amp;diff=289841&amp;oldid=prev"/>
		<updated>2025-10-26T21:34:55Z</updated>

		<summary type="html">&lt;p&gt;update merge notice&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description |Control flow statement for repeating execution until a condition is met}}&lt;br /&gt;
{{use dmy dates|date=September 2025}}&lt;br /&gt;
{{merge|target=Loop (statement)|For loop|Do while loop| discuss=Talk:While loop#Merge proposal|date=September 2025}}&lt;br /&gt;
{{Refimprove|date=October 2016}}&lt;br /&gt;
[[File:While-loop-diagram.svg|thumb|right|While loop flow diagram]]&lt;br /&gt;
{{Loop constructs}}&amp;lt;!-- DO NOT remove. Discuss navigation concept at [[Talk:Do while loop#Helpbox experiment]] --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In [[computer programming]], a &amp;#039;&amp;#039;&amp;#039;while loop&amp;#039;&amp;#039;&amp;#039; is a [[control flow]] [[Statement (computer science)|statement]] that allows code to be executed repeatedly based on a [[Boolean data type|Boolean]] condition. The while loop can be thought of as a repeating [[Conditional (computer programming)|if statement]].&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
A while consists of a block of code and a conditional expression.&amp;lt;ref name=&amp;quot;:0&amp;quot;&amp;gt;{{cite web|url=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html|title=The while and do-while Statements (The Java Tutorials &amp;gt; Learning the Java Language &amp;gt; Language Basics)|website=Dosc.oracle.com|access-date=2016-10-21}}&amp;lt;/ref&amp;gt; The conditional is evaluated, and if true,&amp;lt;ref name=&amp;quot;:0&amp;quot; /&amp;gt; the block of code is executed. This repeats until the conditional becomes [[False (logic)|false]]. Because the while loop checks the conditional before the block is executed, the control structure is also known as a &amp;#039;&amp;#039;pre-test loop&amp;#039;&amp;#039;. In contrast, [[do while loop|do-while loop]] tests the conditional &amp;#039;&amp;#039;after&amp;#039;&amp;#039; the block.&lt;br /&gt;
&lt;br /&gt;
For example, in the languages [[C (programming language)|C]], [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]],&amp;lt;ref&amp;gt;{{cite web |url=http://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx |title=while (C# reference)|website=Msdn.microsoft.com|access-date=2016-10-21}}&amp;lt;/ref&amp;gt; [[Objective-C]], and [[C++]], (which [[Polyglot (computing)|use the same syntax]] in this case), the code fragment&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int x = 0;&lt;br /&gt;
while (x &amp;lt; 5) {&lt;br /&gt;
    printf (&amp;quot;x = %d\n&amp;quot;, x);&lt;br /&gt;
    x++;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
first checks whether x is less than 5, which it is, so the loop body is entered, where {{code |printf()}} is called and x is incremented by 1. After completing the statements in the loop body, the condition, (x &amp;lt; 5), is checked again, and the loop is executed again. This process repeats until x has the value 5.&lt;br /&gt;
&lt;br /&gt;
The condition can always valuate as true to create an [[infinite loop]]. In this case, there may be a early-exit control structure (such as a [[Control flow|break]] statement) that controls termination of the loop. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
while (true) {&lt;br /&gt;
    // do complicated stuff&lt;br /&gt;
    if (someCondition)&lt;br /&gt;
        break;&lt;br /&gt;
    // more stuff&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
These while loops calculate the [[factorial]] of 5:&lt;br /&gt;
&lt;br /&gt;
===ActionScript 3===&lt;br /&gt;
{{Further|ActionScript 3.0}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;actionscript&amp;quot;&amp;gt;&lt;br /&gt;
var counter: int = 5;&lt;br /&gt;
var factorial: int = 1;&lt;br /&gt;
&lt;br /&gt;
while (counter &amp;gt; 1) {&lt;br /&gt;
    factorial *= counter;&lt;br /&gt;
    counter--;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Printf(&amp;quot;Factorial = %d&amp;quot;, factorial);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Ada===&lt;br /&gt;
{{Further|Ada (programming language)}}&lt;br /&gt;
{{Wikibooks|Ada_Programming|Control}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ada&amp;quot;&amp;gt;&lt;br /&gt;
with Ada.Integer_Text_IO;&lt;br /&gt;
&lt;br /&gt;
procedure Factorial is&lt;br /&gt;
    Counter   : Integer := 5;&lt;br /&gt;
    Factorial : Integer := 1;&lt;br /&gt;
begin&lt;br /&gt;
    while Counter &amp;gt; 1 loop&lt;br /&gt;
        Factorial := Factorial * Counter;&lt;br /&gt;
        Counter   := Counter - 1;&lt;br /&gt;
    end loop;&lt;br /&gt;
&lt;br /&gt;
    Ada.Integer_Text_IO.Put (Factorial);&lt;br /&gt;
end Factorial;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===APL===&lt;br /&gt;
{{Further|APL (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;apl&amp;quot;&amp;gt;&lt;br /&gt;
counter ← 5&lt;br /&gt;
factorial ← 1&lt;br /&gt;
&lt;br /&gt;
:While counter &amp;gt; 1&lt;br /&gt;
    factorial ×← counter&lt;br /&gt;
    counter -← 1&lt;br /&gt;
:EndWhile&lt;br /&gt;
&lt;br /&gt;
⎕ ← factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or simply&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;apl&amp;quot;&amp;gt;&lt;br /&gt;
!5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AutoHotkey===&lt;br /&gt;
{{Further|AutoHotkey}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autohotkey&amp;quot;&amp;gt;&lt;br /&gt;
counter := 5&lt;br /&gt;
factorial := 1&lt;br /&gt;
&lt;br /&gt;
While counter &amp;gt; 1&lt;br /&gt;
    factorial *= counter--&lt;br /&gt;
&lt;br /&gt;
MsgBox % factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Small Basic===&lt;br /&gt;
{{Further|Microsoft Small Basic}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;vbnet&amp;quot;&amp;gt;&lt;br /&gt;
counter = 5    &amp;#039; Counter = 5&lt;br /&gt;
factorial = 1  &amp;#039; initial value of variable &amp;quot;factorial&amp;quot;&lt;br /&gt;
&lt;br /&gt;
While counter &amp;gt; 1&lt;br /&gt;
    factorial = factorial * counter&lt;br /&gt;
    counter = counter - 1&lt;br /&gt;
EndWhile&lt;br /&gt;
&lt;br /&gt;
TextWindow.WriteLine(&amp;quot;Factorial = &amp;quot; + factorial)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Visual Basic===&lt;br /&gt;
{{Further|Visual Basic}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;vbnet&amp;quot;&amp;gt;&lt;br /&gt;
Dim counter As Integer = 5    &amp;#039; init variable and set value&lt;br /&gt;
Dim factorial As Integer = 1  &amp;#039; initialize factorial variable&lt;br /&gt;
&lt;br /&gt;
Do While counter &amp;gt; 1&lt;br /&gt;
    factorial = factorial * counter&lt;br /&gt;
    counter = counter - 1&lt;br /&gt;
Loop     &amp;#039; program goes here, until counter = 0&lt;br /&gt;
&lt;br /&gt;
&amp;#039;Debug.Print factorial         &amp;#039; Console.WriteLine(factorial) in Visual Basic .NET&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Bourne (Unix) shell===&lt;br /&gt;
{{Further|Bourne shell}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
counter=5&lt;br /&gt;
factorial=1&lt;br /&gt;
while [ $counter -gt 1 ]; do&lt;br /&gt;
    factorial=$((factorial * counter))&lt;br /&gt;
    counter=$((counter - 1))&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
echo $factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===C, C++===&lt;br /&gt;
{{Further|C (programming language)|C++}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int main() {&lt;br /&gt;
    int counter = 5;&lt;br /&gt;
    int factorial = 1;&lt;br /&gt;
&lt;br /&gt;
    while (counter &amp;gt; 1) {&lt;br /&gt;
        factorial *= counter--;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    printf(&amp;quot;%d&amp;quot;, factorial);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ColdFusion Markup Language (CFML)===&lt;br /&gt;
{{Further|ColdFusion Markup Language}}&lt;br /&gt;
&lt;br /&gt;
====Script syntax====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cfs&amp;quot;&amp;gt;&lt;br /&gt;
counter = 5;&lt;br /&gt;
factorial = 1;&lt;br /&gt;
&lt;br /&gt;
while (counter &amp;gt; 1) {&lt;br /&gt;
    factorial *= counter--;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
writeOutput(factorial);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Tag syntax====&lt;br /&gt;
{{Further|Tag (programming)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=CFM&amp;gt;&lt;br /&gt;
&amp;lt;cfset counter = 5&amp;gt;&lt;br /&gt;
&amp;lt;cfset factorial = 1&amp;gt;&lt;br /&gt;
&amp;lt;cfloop condition=&amp;quot;counter GT 1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;cfset factorial *= counter--&amp;gt;&lt;br /&gt;
&amp;lt;/cfloop&amp;gt;&lt;br /&gt;
&amp;lt;cfoutput&amp;gt;#factorial#&amp;lt;/cfoutput&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Fortran===&lt;br /&gt;
{{Further|Fortran}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;fortran&amp;quot;&amp;gt;&lt;br /&gt;
program FactorialProg&lt;br /&gt;
    integer :: counter = 5&lt;br /&gt;
    integer :: factorial = 1&lt;br /&gt;
&lt;br /&gt;
    do while (counter &amp;gt; 1)&lt;br /&gt;
        factorial = factorial * counter&lt;br /&gt;
        counter = counter - 1&lt;br /&gt;
    end do&lt;br /&gt;
&lt;br /&gt;
    print *, factorial&lt;br /&gt;
end program FactorialProg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Go===&lt;br /&gt;
{{Further|Go (programming language)}}&lt;br /&gt;
Go has no while statement, but it has the function of a for statement when omitting some elements of the for statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;go&amp;quot;&amp;gt;&lt;br /&gt;
counter, factorial := 5, 1&lt;br /&gt;
&lt;br /&gt;
for counter &amp;gt; 1 {&lt;br /&gt;
	counter, factorial = counter-1, factorial*counter&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Java, C#, D===&lt;br /&gt;
{{Further|Java (programming language)|C Sharp (programming language)|D (programming language)}}&lt;br /&gt;
The code for the loop is the same for Java, C# and D:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
int counter = 5;&lt;br /&gt;
int factorial = 1;&lt;br /&gt;
&lt;br /&gt;
while (counter &amp;gt; 1) {&lt;br /&gt;
    factorial *= counter--;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===JavaScript===&lt;br /&gt;
{{Further|JavaScript}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
let counter = 5;&lt;br /&gt;
let factorial = 1;&lt;br /&gt;
&lt;br /&gt;
while (counter &amp;gt; 1)&lt;br /&gt;
    factorial *= counter--;&lt;br /&gt;
&lt;br /&gt;
console.log(factorial);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kotlin ===&lt;br /&gt;
{{Main|Kotlin (programming language)}}&lt;br /&gt;
Source:&amp;lt;ref&amp;gt;{{cite web |title=Conditions and loops {{!}} Kotlin |url=https://kotlinlang.org/docs/control-flow.html#while-loops |website=Kotlin Help |access-date=21 October 2025}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;kotlin&amp;quot;&amp;gt;&lt;br /&gt;
var counter = 5&lt;br /&gt;
var factorial = 1&lt;br /&gt;
&lt;br /&gt;
while (counter &amp;gt; 1) {&lt;br /&gt;
    factorial = factorial * counter&lt;br /&gt;
    counter = counter - 1&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
println(factorial)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
{{Further|Lua (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
counter = 5&lt;br /&gt;
factorial = 1&lt;br /&gt;
&lt;br /&gt;
while counter &amp;gt; 1 do&lt;br /&gt;
  factorial = factorial * counter&lt;br /&gt;
  counter = counter - 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
print(factorial)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===MATLAB, Octave===&lt;br /&gt;
{{Further|MATLAB|GNU Octave}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&lt;br /&gt;
counter = 5;&lt;br /&gt;
factorial = 1;&lt;br /&gt;
&lt;br /&gt;
while (counter &amp;gt; 0)&lt;br /&gt;
    factorial = factorial * counter;      %Multiply&lt;br /&gt;
    counter = counter - 1;                %Decrement&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Mathematica===&lt;br /&gt;
{{Further|Wolfram Mathematica|Wolfram Language}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;mathematica&amp;quot;&amp;gt;&lt;br /&gt;
Block[{counter=5,factorial=1},  (*localize counter and factorial*)&lt;br /&gt;
    While[counter&amp;gt;1,            (*While loop*)&lt;br /&gt;
        factorial*=counter;     (*Multiply*)&lt;br /&gt;
        counter--;              (*Decrement*)&lt;br /&gt;
    ];&lt;br /&gt;
&lt;br /&gt;
    factorial&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Oberon, Oberon-2, Oberon-07, Component Pascal===&lt;br /&gt;
{{Further|Oberon (programming language)|Oberon-2|Oberon-07|Component Pascal}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cp&amp;quot;&amp;gt;&lt;br /&gt;
MODULE Factorial;&lt;br /&gt;
IMPORT Out;&lt;br /&gt;
VAR&lt;br /&gt;
    Counter, Factorial: INTEGER;&lt;br /&gt;
BEGIN&lt;br /&gt;
    Counter := 5;&lt;br /&gt;
    Factorial := 1;&lt;br /&gt;
&lt;br /&gt;
    WHILE Counter &amp;gt; 1 DO&lt;br /&gt;
        Factorial := Factorial * Counter;&lt;br /&gt;
        DEC(Counter)&lt;br /&gt;
    END;&lt;br /&gt;
&lt;br /&gt;
    Out.Int(Factorial,0)&lt;br /&gt;
END Factorial.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Maya Embedded Language===&lt;br /&gt;
{{Further|Maya Embedded Language}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
int $counter = 5;&lt;br /&gt;
int $factorial = 1;&lt;br /&gt;
&lt;br /&gt;
while ($counter &amp;gt; 1) {&lt;br /&gt;
    $factorial *= $counter;&lt;br /&gt;
    $counter -= 1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Factorial = &amp;quot; + $factorial + &amp;quot;\n&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Nim===&lt;br /&gt;
{{Further|Nim (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;nim&amp;quot;&amp;gt;&lt;br /&gt;
var&lt;br /&gt;
  counter = 5            # Set counter value to 5&lt;br /&gt;
  factorial = 1          # Set factorial value to 1&lt;br /&gt;
&lt;br /&gt;
while counter &amp;gt; 1:       # While counter is greater than 0&lt;br /&gt;
    factorial *= counter # Set new value of factorial to counter.&lt;br /&gt;
    dec counter          # Set the counter to counter - 1.&lt;br /&gt;
&lt;br /&gt;
echo factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non-terminating while loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;nim&amp;quot;&amp;gt;&lt;br /&gt;
while true:&lt;br /&gt;
  echo &amp;quot;Help! I&amp;#039;m stuck in a loop!&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Pascal===&lt;br /&gt;
{{Further|Pascal (programming language)}}&lt;br /&gt;
Pascal has two forms of the while loop, {{code| while}} and {{code| repeat-until}}. {{code| while}} repeats one statement (unless enclosed in a begin-end block) as long as the condition is true. {{code| repeat-until}} repetitively executes a block of one or more statements until the a condition is false. The main difference between the two is that {{code| while}} executes zero times if the condition is initially false, whereas {{code| repeat-until}} executes at least once.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;pascal&amp;quot;&amp;gt;&lt;br /&gt;
program Factorial1;&lt;br /&gt;
var&lt;br /&gt;
    Fv: integer;&lt;br /&gt;
&lt;br /&gt;
    procedure fact(counter:integer);&lt;br /&gt;
    var&lt;br /&gt;
        Factorial: integer;&lt;br /&gt;
&lt;br /&gt;
    begin&lt;br /&gt;
         Factorial := 1;&lt;br /&gt;
&lt;br /&gt;
         while Counter &amp;gt; 1 do&lt;br /&gt;
         begin&lt;br /&gt;
             Factorial := Factorial * Counter;&lt;br /&gt;
             Counter := Counter - 1&lt;br /&gt;
         end;&lt;br /&gt;
&lt;br /&gt;
         WriteLn(Factorial)&lt;br /&gt;
     end;&lt;br /&gt;
&lt;br /&gt;
begin&lt;br /&gt;
    Write(&amp;#039;Enter a number to return its factorial: &amp;#039;);&lt;br /&gt;
    readln(fv);&lt;br /&gt;
    repeat&lt;br /&gt;
         fact(fv);&lt;br /&gt;
         Write(&amp;#039;Enter another number to return its factorial (or 0 to quit): &amp;#039;);&lt;br /&gt;
     until fv=0;&lt;br /&gt;
end.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Perl===&lt;br /&gt;
{{Further|Perl}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
my $counter   = 5;&lt;br /&gt;
my $factorial = 1;&lt;br /&gt;
&lt;br /&gt;
while ($counter &amp;gt; 1) {&lt;br /&gt;
    $factorial *= $counter--; # Multiply, then decrement&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
print $factorial;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;While&amp;#039;&amp;#039; loops are frequently used for reading data line by line (as defined by the &amp;lt;code&amp;gt;$/&amp;lt;/code&amp;gt; line separator) from open filehandles:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
open IN, &amp;quot;&amp;lt;test.txt&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
while (&amp;lt;IN&amp;gt;) {&lt;br /&gt;
    print;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
close IN;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===PHP===&lt;br /&gt;
{{Further|PHP}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$counter = 5;&lt;br /&gt;
$factorial = 1;&lt;br /&gt;
&lt;br /&gt;
while ($counter &amp;gt; 1) {&lt;br /&gt;
    $factorial *= $counter--; // Multiply, then decrement.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
echo $factorial;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===PL/I===&lt;br /&gt;
{{Further|PL/I}}&lt;br /&gt;
The PL/I &amp;lt;code&amp;gt;DO&amp;lt;/code&amp;gt; statement can act as either a [[for loop]], a while loop, or a do until loop.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rexx&amp;quot;&amp;gt;&lt;br /&gt;
declare counter   fixed initial(5);&lt;br /&gt;
declare factorial fixed initial(1);&lt;br /&gt;
&lt;br /&gt;
do while(counter &amp;gt; 1)&lt;br /&gt;
    factorial = factorial * counter;&lt;br /&gt;
    counter = counter - 1;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Python===&lt;br /&gt;
{{Further|Python (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
counter = 5                           # Set the value to 5&lt;br /&gt;
factorial = 1                         # Set the value to 1&lt;br /&gt;
&lt;br /&gt;
while counter &amp;gt; 1:                    # While counter(5) is greater than 0&lt;br /&gt;
    factorial *= counter              # Set new value of factorial to counter.&lt;br /&gt;
    counter -= 1                      # Set the counter to counter - 1.&lt;br /&gt;
&lt;br /&gt;
print(factorial)                      # Print the value of factorial.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Non-terminating while loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
while True:&lt;br /&gt;
    print(&amp;quot;Help! I&amp;#039;m stuck in a loop!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Racket===&lt;br /&gt;
{{Further|Racket (programming language)|Scheme (programming language)}}&lt;br /&gt;
In Racket, as in other [[Scheme (programming language)|Scheme]] implementations, a &amp;#039;&amp;#039;named-let&amp;#039;&amp;#039; is a popular way to implement loops:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;racket&amp;quot;&amp;gt;&lt;br /&gt;
#lang racket&lt;br /&gt;
(define counter 5)&lt;br /&gt;
(define factorial 1)&lt;br /&gt;
(let loop ()&lt;br /&gt;
    (when (&amp;gt; counter 1)&lt;br /&gt;
        (set! factorial (* factorial counter))&lt;br /&gt;
        (set! counter (sub1 counter))&lt;br /&gt;
        (loop)))&lt;br /&gt;
(displayln factorial)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Using a macro system, implementing a &amp;#039;&amp;#039;while&amp;#039;&amp;#039; loop is a trivial exercise (commonly used to introduce macros):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;racket&amp;quot;&amp;gt;&lt;br /&gt;
#lang racket&lt;br /&gt;
(define-syntax-rule (while test body ...) ; implements a while loop&lt;br /&gt;
    (let loop () (when test body ... (loop))))&lt;br /&gt;
(define counter 5)&lt;br /&gt;
(define factorial 1)&lt;br /&gt;
(while (&amp;gt; counter 1)&lt;br /&gt;
    (set! factorial (* factorial counter))&lt;br /&gt;
    (set! counter (sub1 counter)))&lt;br /&gt;
(displayln factorial)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
However, an imperative programming style is often discouraged in Scheme and Racket.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
{{Further|Ruby (programming language)}}&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:48%; float:left; margin-right:2%;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
# Count Down Variant&lt;br /&gt;
counter = 5&lt;br /&gt;
factorial = 1&lt;br /&gt;
&lt;br /&gt;
while counter &amp;gt; 1&lt;br /&gt;
  factorial *= counter&lt;br /&gt;
  counter -= 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;width:48%; float:left;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
# Count Up Variant&lt;br /&gt;
counter = 2&lt;br /&gt;
factorial = 1&lt;br /&gt;
&lt;br /&gt;
while counter &amp;lt;= 5&lt;br /&gt;
  factorial *= counter&lt;br /&gt;
  counter += 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
puts factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Rust===&lt;br /&gt;
{{Further|Rust (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn main() {&lt;br /&gt;
    let mut counter = 5;&lt;br /&gt;
    let mut factorial = 1;&lt;br /&gt;
&lt;br /&gt;
    while counter &amp;gt; 1 {&lt;br /&gt;
        factorial *= counter;&lt;br /&gt;
        counter -= 1;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    println!(&amp;quot;{}&amp;quot;, factorial);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
{{Further|Smalltalk}}&lt;br /&gt;
Contrary to other languages, in Smalltalk a &amp;#039;&amp;#039;while&amp;#039;&amp;#039; loop is not a [[language construct]] but defined in the class &amp;lt;code&amp;gt;BlockClosure&amp;lt;/code&amp;gt; as a method with one parameter, the body as a [[Closure (computer programming)|closure]], using self as the condition.&lt;br /&gt;
&lt;br /&gt;
Smalltalk also has a corresponding whileFalse: method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;smalltalk&amp;quot;&amp;gt;&lt;br /&gt;
| count factorial |&lt;br /&gt;
count := 5.&lt;br /&gt;
factorial := 1.&lt;br /&gt;
[count &amp;gt; 0] whileTrue:&lt;br /&gt;
    [factorial := factorial * count.&lt;br /&gt;
    count := count - 1].&lt;br /&gt;
Transcript show: factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Swift===&lt;br /&gt;
{{Further|Swift (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;swift&amp;quot;&amp;gt;&lt;br /&gt;
var counter = 5                 // Set the initial counter value to 5&lt;br /&gt;
var factorial = 1               // Set the initial factorial value to 1&lt;br /&gt;
&lt;br /&gt;
while counter &amp;gt; 1 {             // While counter(5) is greater than 0&lt;br /&gt;
    factorial *= counter        // Set new value of factorial to factorial x counter.&lt;br /&gt;
    counter -= 1                // Set the new value of counter to  counter - 1.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
print(factorial)                // Print the value of factorial.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Tcl===&lt;br /&gt;
{{Further|Tcl}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;tcl&amp;quot;&amp;gt;&lt;br /&gt;
set counter 5&lt;br /&gt;
set factorial 1&lt;br /&gt;
&lt;br /&gt;
while {$counter &amp;gt; 1} {&lt;br /&gt;
    set factorial [expr $factorial * $counter]&lt;br /&gt;
    incr counter -1&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
puts $factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===PowerShell===&lt;br /&gt;
{{Further|PowerShell}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$counter = 5&lt;br /&gt;
$factorial = 1&lt;br /&gt;
&lt;br /&gt;
while ($counter) {&lt;br /&gt;
    $factorial *= $counter--&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===While (language)===&lt;br /&gt;
While&amp;lt;ref&amp;gt;{{cite web|url=http://profs.sci.univr.it/~merro/files/WhileExtra_l.pdf |title=Chapter 3: The While programming language |website=Profs.sci.univr.it |access-date=2016-10-21}}&amp;lt;/ref&amp;gt; is a simple programming language constructed from assignments, sequential composition, conditionals, and while statements, used in the theoretical analysis of imperative programming language [[Semantics (computer science)|semantics]].&amp;lt;ref name=&amp;quot;NielsonNielson1999&amp;quot;&amp;gt;{{cite book|author1=Flemming Nielson|author2=Hanne R. Nielson|author3=Chris Hankin|title=Principles of Program Analysis|url=https://books.google.com/books?id=RLjt0xSj8DcC|access-date=29 May 2013|year=1999|publisher=Springer|isbn=978-3-540-65410-0}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite book |first=Valerie |last=Illingworth |date=11 December 1997 |title=Dictionary of Computing |edition=4th |series=Oxford Paperback Reference |publisher=Oxford University Press |isbn=9780192800466 |url-access=registration |url=https://archive.org/details/dictionaryofcomp00illi}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;whiley&amp;quot;&amp;gt;&lt;br /&gt;
C := 5;&lt;br /&gt;
F := 1;&lt;br /&gt;
&lt;br /&gt;
while (C &amp;gt; 1) do&lt;br /&gt;
    F := F * C;&lt;br /&gt;
    C := C - 1;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Do while loop]]&lt;br /&gt;
* [[For loop]]&lt;br /&gt;
* [[Foreach]]&lt;br /&gt;
* [[Primitive recursive function]]&lt;br /&gt;
* [[General recursive function]]&lt;br /&gt;
* [[LOOP (programming language)]] – a programming language with the property that the functions it can compute are exactly the primitive recursive functions&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:While Loop}}&lt;br /&gt;
[[Category:Control flow]]&lt;br /&gt;
[[Category:Iteration in programming]]&lt;br /&gt;
[[Category:Programming language comparisons]]&lt;br /&gt;
&amp;lt;!-- Hidden categories below --&amp;gt;&lt;br /&gt;
[[Category:Articles with example Ada code]]&lt;br /&gt;
[[Category:Articles with example BASIC code]]&lt;br /&gt;
[[Category:Articles with example C code]]&lt;br /&gt;
[[Category:Articles with example C++ code]]&lt;br /&gt;
[[Category:Articles with example C Sharp code]]&lt;br /&gt;
[[Category:Articles with example D code]]&lt;br /&gt;
[[Category:Articles with example Fortran code]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;br /&gt;
[[Category:Articles with example JavaScript code]]&lt;br /&gt;
[[Category:Articles with example MATLAB/Octave code]]&lt;br /&gt;
[[Category:Articles with example Pascal code]]&lt;br /&gt;
[[Category:Articles with example Perl code]]&lt;br /&gt;
[[Category:Articles with example PHP code]]&lt;br /&gt;
[[Category:Articles with example Python (programming language) code]]&lt;br /&gt;
[[Category:Articles with example Racket code]]&lt;br /&gt;
[[Category:Articles with example Ruby code]]&lt;br /&gt;
[[Category:Articles with example Rust code]]&lt;br /&gt;
[[Category:Articles with example Smalltalk code]]&lt;br /&gt;
[[Category:Articles with example Swift code]]&lt;br /&gt;
[[Category:Articles with example Tcl code]]&lt;/div&gt;</summary>
		<author><name>imported&gt;FaviFake</name></author>
	</entry>
</feed>