while loop : while « Statement « JavaScript Tutorial






The while loop can be summarized as while the expression evaluates to true, execute the statements in the loop.

Once the last statement in the loop is executed, go back to the top of the loop and evaluate the expression again.

When the expression evaluates to false, the next line of code following the while loop structure is executed.

Because the expression is evaluated before the loop, it is possible the loop will never be executed.

The format of the while loop looks like the following:

while (expression)
    {
      statement;
    }
<html>
    <SCRIPT LANGUAGE='JavaScript'>
    <!--
    var light = "red";             
    var counter = 1;               
    var carsInLine = new Array();  

    while (counter <= 5)
    {
      document.write("Car ",counter," approaches intersection.<BR>");
      counter++;   //Next car
    }
    //-->
    </SCRIPT>
</html>








3.6.while
3.6.1.while loop
3.6.2.Use integer variable to control the while loop
3.6.3.Check the loop counter for while loop
3.6.4.Use while loop to output all elements in an array
3.6.5.Use Do while loop to reverse a string
3.6.6.Nesting If statement to a while statement
3.6.7.Using While loop to check user input