Loops are the repeated execution of some segment of code for as long as a specific condition remains true.
The simplest form of loop is the "while" loop, which executes it's statement
for as long as its expression remains true:
while (expression) statement
Note that the ()'s that surround the expression are not optional, just like with the if statement. In many ways a while loop is like an if-statement, except that where an if-statement executes its statement only once if the expression is true, a while-loop will continue to execute its statement for as long as its expression is true.
Examples:
The above prints 0 through 9. Remember that 'i++' gives it's value
before incrementing. When i becomes 10, the expression is no longer
true, so the execution moves on to the statements that follow the while loop.
The same as the previous but using a compound statement and moving the increment
of i outside of the console.log() function.
An upside-down variant of while is the do-while loop:
do statement while (expression);
This loop will execute statement at least once, since it done before the expression in the while part is tested. If the expression is true it will go back to the top and do the statement again. For clarity purposes statement should always be a compound statement, i.e. a sequence of one or more statements enclosed in {}'s (curly braces.)
Still prints once, even though expression can never be true.
Still prints 0 - 9.