Changes

Jump to: navigation, search

JavaScript Flow Control and Looping

9 bytes added, 20:58, 19 June 2007
JavaScript ''while'' loops
</pre>
In the above example the ''while'' expression will evaluate whether i is less than 10. If it is already greater than 10 then the code in the braces is skipped and the loop exits without performing any tasks. If it is not greater than 10 the code in the braces is executed and the loop returns to the ''while '' statement and repeats the evaluation of i. This process repeats until i is greater than 10, at which point the loop exits.
=== JavaScript ''do ... while'' loops ===
Think of the ''do ... while'' loop as an inverted ''while'' loop. As shown above, the ''while'' loop evaluates an expression before executing the code contained in the body of the loop. If the expression evaluates to ''false'' on the first check then the code is not executed. The ''do .. while'' loop is provided for situations where you know that the code in your loop will ''always'' need to be executed at least once. For example , you may want to keep asking a user to fill in a field in a prompt dialog until specific text is entered. You know that you have to show the prompt dialog at least once to get a string from the user to evaluate against. The syntax for the ''do ... while'' loop is as follows:
<pre>

Navigation menu