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!
==Overview== The ''while'' construct consists of a block of code and a condition/expression.<ref name=":0">{{cite web|url=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html|title=The while and do-while Statements (The Java Tutorials > Learning the Java Language > Language Basics)|website=Dosc.oracle.com|access-date=2016-10-21}}</ref> The condition/expression is evaluated, and if the condition/expression is ''true'',<ref name=":0" /> the code within all of their following in the block is executed. This repeats until the condition/expression becomes [[False (logic)|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|''do while'' loop]], which tests the condition/expression ''after'' the loop has executed. For example, in the languages [[C (programming language)|C]], [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]],<ref>{{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}}</ref> [[Objective-C]], and [[C++]], (which [[Polyglot (computing)|use the same syntax]] in this case), the code fragment <syntaxhighlight lang="c"> int x = 0; while (x < 5) { printf ("x = %d\n", x); x++; } </syntaxhighlight> first checks whether x is less than 5, which it is, so then the {loop body} 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 (computer science)|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 [[Control flow|break]] statement) that controls termination of the loop. For example: <syntaxhighlight lang="c"> while (true) { // do complicated stuff if (someCondition) break; // more stuff } </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)