About the While statement

You use the while statement to create an execution loop. More specifically, while repeatedly evaluates a statement or statement block until some condition is met. The while statement has the following format:

while( expression )
  statement

While first evaluates expression, and if it is true, executes statement. While repeats this process until expression becomes false. If expression is false when it is first evaluated, statement is never executed.

The following example adds the square of all integers from 1 to 10 (inclusive):

var total=0,n=1;
while(n<=10) {
  total+=n*n;
  n++;
}

In the first evaluation of the expression n<=10, n is equal to 1; so, the expression is true. This means the statement block following while should be executed. The first statement in the block adds the value of n*n to total. The second statement adds one to n. After executing the block, the expression n<=10 is evaluated again. This time, n is equal to 2; so, the expression is still true. This process continues until n is equal to 11. At this point, the conditional expression is false and execution of the while loop ends.