While loop
In most computer programming languages, a while loop is a control structure that allows code to be executed repeatedly based on a given boolean condition.
Related Topics:
Computer programming - Control structure - Boolean
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
The while construct consists of a block of code and a condition. The condition is first evaluated - if the condition is true the code within the block is then executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.
Related Topics:
True - False - Do while loop
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
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 (such as a break statement) that controls termination of the loop.
Related Topics:
Infinite loop - Break
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
For example, in the C programming language, the code fragment
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
x = 0;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
while (x < 3)
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
x++;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
first checks whether x is less than 3, which it is not, so it increments x by 1. It then checks the condition again, and executes again, repeating this process until the variable x has the desired value, 3.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ Table of Content ~
| ► | Introduction |
| ► | Demonstrating while loops |
| ► | See also |
~ What's Hot ~
~ Community ~
| ► | History Forum Come and discuss about History, Civilizations, Historical Events and Figures |
| ► | History Web-Ring A community of sites, blogs and forums dedicated to History. Do not hesitate to submit your site. |
and are licensed under the GNU Free Documentation License.
Lexicon - Privacy Policy - Spiritus-Temporis.com ©2005.