JS While Loop Statement
JavaScript While Loop Statement
JavaScript While Loop Statement
-
While Loop statement is a repetitive statement which is executed after evaluating the condition.
If the resulting value is true the the execution is repetead until becomes false.
Syntax:
while (condition){ If condition is true the the statement is executed } |
Example:
<script type="text/javascript"> <!-- var a = 0; document.write("Start Loop" + "<br />"); while (a < 5){ document.write("Variable a : " + a + "<br />"); a++; } document.write("Stop Loop"); //--> </script> |
The result:
Start Loop Variable a : 0 Variable a : 1 Variable a : 2 Variable a : 3 Variable a : 4 Stop Loop |
JavaScript do…while Loop Statement
-
do…while Loop statement is a repetitive statement that executes the statements and assess the expression.
If the resulting value is true the the execution is repetead until becomes false.
Syntax:
do{ Statement_1 to be executed; Statement_2 to be executed; ... Statement_n to be executed; } while (expression); |
Example:
<script type="text/javascript"> <!-- var a = 0; document.write("Start Loop" + "<br />"); do{ document.write("Variable a : " + a + "<br />"); a++; } while (count < 0); document.write("Stop Loop"); //--> </script> |
The result:
Start Loop Variable a : 0 Stop Loop |