Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
While loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Demonstrating ''while'' loops== These ''while'' loops will calculate the [[factorial]] of the number 5: ===ActionScript 3=== {{Further|ActionScript 3.0}} <syntaxhighlight lang="actionscript"> var counter: int = 5; var factorial: int = 1; while (counter > 1) { factorial *= counter; counter--; } Printf("Factorial = %d", factorial); </syntaxhighlight> ===Ada=== {{Further|Ada (programming language)}} {{Wikibooks|Ada_Programming|Control}} <syntaxhighlight lang="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 Factorial; </syntaxhighlight> ===APL=== {{Further|APL (programming language)}} <syntaxhighlight lang="apl"> counter β 5 factorial β 1 :While counter > 0 factorial Γβ counter counter -β 1 :EndWhile β β factorial </syntaxhighlight> or simply <syntaxhighlight lang="apl"> !5 </syntaxhighlight> ===AutoHotkey=== {{Further|AutoHotkey}} <syntaxhighlight lang="autohotkey"> counter := 5 factorial := 1 While counter > 0 factorial *= counter-- MsgBox % factorial </syntaxhighlight> ===Small Basic=== {{Further|Microsoft Small Basic}} <syntaxhighlight lang="vbnet"> counter = 5 ' Counter = 5 factorial = 1 ' initial value of variable "factorial" While counter > 0 factorial = factorial * counter counter = counter - 1 TextWindow.WriteLine(counter) EndWhile </syntaxhighlight> ===Visual Basic=== {{Further|Visual Basic}} <syntaxhighlight lang="vbnet"> 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(factorial) in Visual Basic .NET </syntaxhighlight> ===Bourne (Unix) shell=== {{Further|Bourne shell}} <syntaxhighlight lang="bash"> counter=5 factorial=1 while [ $counter -gt 0 ]; do factorial=$((factorial * counter)) counter=$((counter - 1)) done echo $factorial </syntaxhighlight> ===C, C++=== {{Further|C (programming language)|C++}} <syntaxhighlight lang="c"> int main() { int count = 5; int factorial = 1; while (count > 1) factorial *= count--; printf("%d", factorial); } </syntaxhighlight> ===ColdFusion Markup Language (CFML)=== {{Further|ColdFusion Markup Language}} ====Script syntax==== <syntaxhighlight lang="cfs"> counter = 5; factorial = 1; while (counter > 1) { factorial *= counter--; } writeOutput(factorial); </syntaxhighlight> ====Tag syntax==== {{Further|Tag (programming)}} <syntaxhighlight lang=CFM> <cfset counter = 5> <cfset factorial = 1> <cfloop condition="counter GT 1"> <cfset factorial *= counter--> </cfloop> <cfoutput>#factorial#</cfoutput> </syntaxhighlight> ===Fortran=== {{Further|Fortran}} <syntaxhighlight lang="fortran"> program FactorialProg integer :: counter = 5 integer :: factorial = 1 do while (counter > 0) factorial = factorial * counter counter = counter - 1 end do print *, factorial end program FactorialProg </syntaxhighlight> ===Go=== {{Further|Go (programming language)}} Go has no ''while'' statement, but it has the function of a ''for'' statement when omitting some elements of the ''for'' statement. <syntaxhighlight lang="go"> counter, factorial := 5, 1 for counter > 1 { counter, factorial = counter-1, factorial*counter } </syntaxhighlight> ===Java, C#, D=== {{Further|Java (programming language)|C Sharp (programming language)|D (programming language)}} The code for the loop is the same for Java, C# and D: <syntaxhighlight lang="csharp"> int counter = 5; int factorial = 1; while (counter > 1) factorial *= counter--; </syntaxhighlight> ===JavaScript=== {{Further|JavaScript}} <syntaxhighlight lang="javascript"> let counter = 5; let factorial = 1; while (counter > 1) factorial *= counter--; console.log(factorial); </syntaxhighlight> ===Lua=== {{Further|Lua (programming language)}} <syntaxhighlight lang="lua"> counter = 5 factorial = 1 while counter > 0 do factorial = factorial * counter counter = counter - 1 end print(factorial) </syntaxhighlight> ===MATLAB, Octave=== {{Further|MATLAB|GNU Octave}} <syntaxhighlight lang="matlab"> counter = 5; factorial = 1; while (counter > 0) factorial = factorial * counter; %Multiply counter = counter - 1; %Decrement end factorial </syntaxhighlight> ===Mathematica=== {{Further|Wolfram Mathematica|Wolfram Language}} <syntaxhighlight lang="mathematica"> Block[{counter=5,factorial=1}, (*localize counter and factorial*) While[counter>0, (*While loop*) factorial*=counter; (*Multiply*) counter--; (*Decrement*) ]; factorial ] </syntaxhighlight> ===Oberon, Oberon-2, Oberon-07, Component Pascal=== {{Further|Oberon (programming language)|Oberon-2|Oberon-07|Component Pascal}} <syntaxhighlight lang="pascal"> MODULE Factorial; IMPORT Out; VAR Counter, Factorial: INTEGER; BEGIN Counter := 5; Factorial := 1; WHILE Counter > 0 DO Factorial := Factorial * Counter; DEC(Counter) END; Out.Int(Factorial,0) END Factorial. </syntaxhighlight> ===Maya Embedded Language=== {{Further|Maya Embedded Language}} <syntaxhighlight lang="perl"> int $counter = 5; int $factorial = 1; int $multiplication; while ($counter > 0) { $multiplication = $factorial * $counter; $counter -= 1; print("Counter is: " + $counter + ", multiplication is: " + $multiplication + "\n"); } </syntaxhighlight> ===Nim=== {{Further|Nim (programming language)}} <syntaxhighlight lang="nim"> var counter = 5 # Set counter value to 5 factorial = 1 # Set factorial value to 1 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 </syntaxhighlight> Non-terminating while loop: <syntaxhighlight lang="nim"> while true: echo "Help! I'm stuck in a loop!" </syntaxhighlight> ===Pascal=== {{Further|Pascal (programming language)}} 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. <syntaxhighlight lang="pascal"> program Factorial1; var Fv: integer; 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. </syntaxhighlight> ===Perl=== {{Further|Perl}} <syntaxhighlight lang="perl"> my $counter = 5; my $factorial = 1; while ($counter > 0) { $factorial *= $counter--; # Multiply, then decrement } print $factorial; </syntaxhighlight> ''While'' loops are frequently used for reading data line by line (as defined by the <code>$/</code> line separator) from open filehandles: <syntaxhighlight lang="perl"> open IN, "<test.txt"; while (<IN>) { print; } close IN; </syntaxhighlight> ===PHP=== {{Further|PHP}} <syntaxhighlight lang="php"> $counter = 5; $factorial = 1; while ($counter > 0) { $factorial *= $counter--; // Multiply, then decrement. } echo $factorial; </syntaxhighlight> ===PL/I=== {{Further|PL/I}} <syntaxhighlight lang="rexx"> declare counter fixed initial(5); declare factorial fixed initial(1); do while(counter > 0) factorial = factorial * counter; counter = counter - 1; end; </syntaxhighlight> ===Python=== {{Further|Python (programming language)}} <syntaxhighlight lang="python"> counter = 5 # Set the value to 5 factorial = 1 # Set the value to 1 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. </syntaxhighlight> Non-terminating while loop: <syntaxhighlight lang="python"> while True: print("Help! I'm stuck in a loop!") </syntaxhighlight> ===Racket=== {{Further|Racket (programming language)|Scheme (programming language)}} In Racket, as in other [[Scheme (programming language)|Scheme]] implementations, a ''named-let'' is a popular way to implement loops: <syntaxhighlight lang="racket"> #lang racket (define counter 5) (define factorial 1) (let loop () (when (> counter 0) (set! factorial (* factorial counter)) (set! counter (sub1 counter)) (loop))) (displayln factorial) </syntaxhighlight> Using a macro system, implementing a ''while'' loop is a trivial exercise (commonly used to introduce macros): <syntaxhighlight lang="racket"> #lang racket (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) </syntaxhighlight> However, an imperative programming style is often discouraged in Scheme and Racket. ===Ruby=== {{Further|Ruby (programming language)}} <syntaxhighlight lang="ruby"> # Calculate the factorial of 5 i = 1 factorial = 1 while i <= 5 factorial *= i i += 1 end puts factorial </syntaxhighlight> ===Rust=== {{Further|Rust (programming language)}} <syntaxhighlight lang="rust"> fn main() { let mut counter = 5; let mut factorial = 1; while counter > 1 { factorial *= counter; counter -= 1; } println!("{}", factorial); } </syntaxhighlight> ===Smalltalk=== {{Further|Smalltalk}} Contrary to other languages, in Smalltalk a ''while'' loop is not a [[language construct]] but defined in the class <code>BlockClosure</code> as a method with one parameter, the body as a [[Closure (computer programming)|closure]], using self as the condition. Smalltalk also has a corresponding whileFalse: method. <syntaxhighlight lang="smalltalk"> | count factorial | count := 5. factorial := 1. [count > 0] whileTrue: [factorial := factorial * count. count := count - 1]. Transcript show: factorial </syntaxhighlight> ===Swift=== {{Further|Swift (programming language)}} <syntaxhighlight lang="swift"> var counter = 5 // Set the initial counter value to 5 var factorial = 1 // Set the initial factorial value to 1 while counter > 0 { // While counter(5) is greater than 0 factorial *= counter // Set new value of factorial to factorial x counter. counter -= 1 // Set the new value of counter to counter - 1. } print(factorial) // Print the value of factorial. </syntaxhighlight> ===Tcl=== {{Further|Tcl}} <syntaxhighlight lang="tcl"> set counter 5 set factorial 1 while {$counter > 0} { set factorial [expr $factorial * $counter] incr counter -1 } puts $factorial </syntaxhighlight> ===VEX=== {{Further|VEX prefix}} <syntaxhighlight lang="csharp"> int counter = 5; int factorial = 1; while (counter > 1) factorial *= counter--; printf("%d", factorial); </syntaxhighlight> ===PowerShell=== {{Further|PowerShell}} <syntaxhighlight lang="powershell"> $counter = 5 $factorial = 1 while ($counter) { $factorial *= $counter-- } $factorial </syntaxhighlight> ===While (language)=== While<ref>{{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}}</ref> 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]].<ref name="NielsonNielson1999">{{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}}</ref><ref>{{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}}</ref> <syntaxhighlight lang="whiley"> C := 5; F := 1; while (C > 1) do F := F * C; C := C - 1; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)