Javascript while








while

The while statement is a pretest loop. Here's the syntax:

while(expression) {
   statement
}

The following code has a while loop which use an integer to control the condition.

The variable i starts out equal to 0 and is incremented by two each time through the loop. As long as the variable is less than 10, the loop will continue.


var i = 0; 
while (i < 10) { 
   i += 2; 
   console.log(i);
} 

The code above generates the following result.





do...while

The do-while statement is a post-test loop. The body of the loop is always executed at least once.

Here's the syntax:

do { 
    statement 
} while (expression);

And here's an example of its usage:


var i = 0; 
do { 
    i += 2; 
    document.writeln(i);
} while (i < 10); 

The code above generates the following result.