While loop


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.

Overview

The while construct consists of a block of code and a condition/expression. The condition/expression is evaluated, and if the condition/expression is true, the code within all of their following in the block is executed. This repeats until the condition/expression becomes false. Because the while loop checks the condition/expression before the block is executed, the control structure is often also known as a pre-test loop. Compare this with the do while loop, which tests the condition/expression after the loop has executed.
For example, in the C programming language, the code fragment

int x = 0;
while

first checks whether x is less than 5, which it is, so then the is entered, where the printf function is run and x is incremented by 1. After completing all the statements in the loop body, the condition,, is checked again, and the loop is executed again, this process repeating until the variable x has the value 5.
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 that controls termination of the loop.
For example:

while

Demonstrating ''while'' loops

These while loops will calculate the factorial of the number 5:

ActionScript 3">ActionScript 3.0">ActionScript 3


var counter: int = 5;
var factorial: int = 1;
while
Printf;

Ada">Ada (programming language)">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 ;
end Factorial;

[AutoHotkey]


counter := 5
factorial := 1
While counter > 0
factorial *= counter--
MsgBox % factorial

[Microsoft Small Basic]


counter = 5 ' Counter = 5
factorial = 1 ' initial value of variable "factorial"
While counter > 0
factorial = factorial * counter
counter = counter - 1
TextWindow.WriteLine
EndWhile

[Visual Basic]


Dim counter As Integer = 5 ' init variable and set value
Dim factorial As Integer = 1 ' initialize factorial variable
Do While counter > 0
factorial = factorial * counter
counter = counter - 1
Loop ' program goes here, until counter = 0
'Debug.Print factorial ' Console.WriteLine in Visual Basic.NET

Bourne (Unix) shell">Bourne shell">Bourne (Unix) shell


counter=5
factorial=1
while ; do
factorial=$)
counter=$)
done
echo $factorial

C">C (programming language)">C or [C++]


int main

Script syntax


counter = 5;
factorial = 1;
while
writeOutput;

Tag syntax







#factorial#

[Fortran]


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

Java">Java (programming language)">Java, C#">C Sharp (programming language)">C#, D">D (programming language)">D

The code for the loop is the same for Java, C# and D:

int counter = 5;
int factorial = 1;
while
factorial *= counter--;

[JavaScript]


let counter = 5;
let factorial = 1;
while
factorial *= counter--;
console.log;

Lua">Lua (programming language)">Lua


counter = 5
factorial = 1
while counter > 0 do
factorial = factorial * counter
counter = counter - 1
end
print

[MATLAB] & [GNU Octave]


counter = 5;
factorial = 1;
while
factorial = factorial * counter; %Multiply
counter = counter - 1; %Decrement
end
factorial

[Mathematica]


Block;
factorial

Oberon">Oberon (programming language)">Oberon, [Oberon-2 (programming language)], [Oberon-07], or [Component Pascal]


MODULE Factorial;
IMPORT Out;
VAR
Counter, Factorial: INTEGER;
BEGIN
Counter := 5;
Factorial := 1;
WHILE Counter > 0 DO
Factorial := Factorial * Counter;
DEC
END;

Out.Int
END Factorial.

[Maya Embedded Language]


int $counter = 5;
int $factorial = 1;
int $multiplication;
while

Pascal">Pascal programming language">Pascal


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

while Counter > 0 do
begin
Factorial := Factorial * Counter;
Counter := Counter - 1
end;
WriteLn
end.

[Perl]


my $counter = 5;
my $factorial = 1;
while
print $factorial;

While loops are frequently used for reading data line by line from open filehandles:

open IN, "while
close IN;

[PHP]


$counter = 5;
$factorial = 1;
while
print $factorial;

[PL/I]


declare counter fixed initial;
declare factorial fixed initial;
do while
factorial = factorial * counter;
counter = counter - 1;
end;

Python">Python (programming language)">Python


counter = 5 # Set the value to 5
factorial = 1 # Set the value to 1
while counter > 0: # While counter is greater than 0
factorial *= counter # Set new value of factorial to counter.
counter -= 1 # Set the counter to counter - 1.
print # Print the value of factorial.

Non-terminating while loop:

while True:
print

Racket">Racket (programming language)">Racket

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

  1. lang racket



))

Using a macro system, implementing a while loop is a trivial exercise :

  1. lang racket
; implements a while loop
)

)

But note that an imperative programming style is often discouraged in Racket.

Ruby">Ruby (programming language)">Ruby


  1. Calculate the factorial of 5
i = 1
factorial = 1
while i <= 5
factorial *= i
i += 1
end
puts factorial

Rust">Rust (programming language)">Rust


fn main

[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.
Smalltalk also has a corresponding whileFalse: method.

count := 5.
factorial := 1.
whileTrue:
.
Transcript show: factorial

Swift">Swift (programming language)">Swift


var counter = 5 // Set the initial counter value to 5
var factorial = 1 // Set the initial factorial value to 1
while counter > 0
print // Print the value of factorial.

[Tcl]


set counter 5
set factorial 1
while
puts $factorial

VEX">VEX prefix">VEX


int counter = 5;
int factorial = 1;
while
factorial *= counter--;
printf;

[PowerShell]


$counter = 5
$factorial = 1
while
$factorial

While programming language

The While programming language is a simple programming language constructed from assignments, sequential composition, conditionals and while statements, used in the theoretical analysis of imperative programming language semantics.

C := 5;
F := 1;
while do
F := F * C;
C := C - 1;