Repetition, or Loops
There are several ways to execute a statement or block of statements repeatedly. In general, repetitive execution is called looping . It is typically controlled by a test of some variable, the value of which is changed each time the loop is executed. There are several types of loops: for loops, for...in loops, while loops, and do...while loops, each behaves slightly differently and it is up to the programmer to choose the most appropriate
The for statement specifies a counter variable, a test condition, and an action that updates the counter. Just before each time the loop is executed (this is called single pass or one iteration of the loop), the condition is tested. After the loop is executed, the counter variable is updated before the next iteration begins.
This is useful if the block of code is to be executed a fixed or known number of times
The while loop is very similar to a for loop. The difference is that a while loop does not have a built-in counter variable or update expression. If you already have some changing condition that is reflected in the value assigned to a variable, and you want to use it to control repetitive execution of a statement or block of statements, use a while loop.
Notice that if the condition for a while loop is not initially met, the loop is never executed at all. If the test condition is always met, an infinite loop results. While the former may be desirable in certain cases, the latter rarely is, so take care when writing your loop conditions, by making sure the test condition changes for each pass.
Note Because while loops do not have explicit built-in counter variables, they are even more vulnerable to infinite looping than the other types. Moreover, partly because it is not necessarily easy to discover where or when the loop condition is updated, it is only too easy to write a while loop in which the condition, in fact, never does get updated. You should be extremely careful when you design while loops.
The syntax of a do ... while loop puts the test condition at the end of the block. In this case the block of code is always executed at least once, before the test condition is applied.
Again you must be very careful that the test condition changes, for each pass of the loop