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/expression.[1] 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 languages C, Java, C#,[2] Objective-C, and C++, (which use the same syntax in this case), the code fragment
while (x < 5)
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, (x < 5), is checked again, and the loop is executed again, this process repeating until the variable x has the value 5.
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:
These while loops will calculate the factorial of the number 5:
while (counter > 1)
Printf("Factorial = %d", factorial);
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 Factorial;
While counter > 0 factorial ×← counter counter -← 1
EndWhile
⎕ ← factorial
or simply
While counter > 0 factorial *= counter--
MsgBox % factorial
While counter > 0 factorial = factorial * counter counter = counter - 1 TextWindow.WriteLine(counter)EndWhile
Do While counter > 0 factorial = factorial * counter counter = counter - 1Loop ' program goes here, until counter = 0
'Debug.Print factorial ' Console.WriteLine(factorial) in Visual Basic .NET
echo $factorial
while (counter > 1)
writeOutput(factorial);
do while (counter > 0) factorial = factorial * counter counter = counter - 1 end do
print *, factorialend program FactorialProg
Go has no while statement, but it has the function of a for statement when omitting some elements of the for statement.
for counter > 1
The code for the loop is the same for Java, C# and D:
while (counter > 1) factorial *= counter--;
while (counter > 1) factorial *= counter--;
console.log(factorial);
while counter > 0 do factorial = factorial * counter counter = counter - 1end
print(factorial)
while (counter > 0) factorial = factorial * counter; %Multiply counter = counter - 1; %Decrementend
factorial
factorial]
WHILE Counter > 0 DO Factorial := Factorial * Counter; DEC(Counter) END;
Out.Int(Factorial,0)END Factorial.
int $multiplication;
while ($counter > 0)
while counter > 0: # While counter is greater than 0 factorial *= counter # Set new value of factorial to counter. dec counter # Set the counter to counter - 1.
echo factorial
Non-terminating while loop:
Pascal has two forms of the while loop, while and repeat. While repeats one statement (unless enclosed in a begin-end block) as long as the condition is true. The repeat statement repetitively executes a block of one or more statements through an until statement and continues repeating unless the condition is false. The main difference between the two is the while loop may execute zero times if the condition is initially false, the repeat-until loop always executes at least once.
procedure fact(counter:integer); var Factorial: integer;
begin Factorial := 1;
while Counter > 0 do begin Factorial := Factorial * Counter; Counter := Counter - 1 end;
WriteLn(Factorial) end;
begin Write('Enter a number to return its factorial: '); readln(fv); repeat fact(fv); Write('Enter another number to return its factorial (or 0 to quit): '); until fv=0;end.
while ($counter > 0)
print $factorial;
While loops are frequently used for reading data line by line (as defined by the $/
line separator) from open filehandles:
while ( close IN;
while ($counter > 0)
echo $factorial;
do while(counter > 0) factorial = factorial * counter; counter = counter - 1;end;
while counter > 0: # While counter(5) is greater than 0 factorial *= counter # Set new value of factorial to counter. counter -= 1 # Set the counter to counter - 1.
print(factorial) # Print the value of factorial.
Non-terminating while loop:
In Racket, as in other Scheme implementations, a named-let is a popular way to implement loops:
(define counter 5)(define factorial 1)(let loop (when (> counter 0) (set! factorial (* factorial counter)) (set! counter (sub1 counter)) (loop)))(displayln factorial)Using a macro system, implementing a while loop is a trivial exercise (commonly used to introduce macros):
(define-syntax-rule (while test body ...) ; implements a while loop (let loop (when test body ... (loop))))(define counter 5)(define factorial 1)(while (> counter 0) (set! factorial (* factorial counter)) (set! counter (sub1 counter)))(displayln factorial)However, an imperative programming style is often discouraged in Scheme and Racket.
i = 1factorial = 1
while i <= 5 factorial *= i i += 1end
puts factorial
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.
while counter > 0
print(factorial) // Print the value of factorial.
while
puts $factorial
while (counter > 1) factorial *= counter--;
printf("%d", factorial);
while ($counter)
$factorial
While[3] is a simple programming language constructed from assignments, sequential composition, conditionals, and while statements, used in the theoretical analysis of imperative programming language semantics.[4] [5]
while (C > 1) do F := F * C; C := C - 1;