Loops

  Use loops to execute a block of code to run multiple times.   

  In JavaScript there are two different kind of loops:


 The for loop:

for ( variable = startvalue; variable <= endvalue; variable = variable + increment )
{
    code to be executed...;
    more code to be executed...;
}


<HTML> <HEAD> <TITLE> for loop </TITLE> < SCRIPT LANGUAGE="JavaScript"> function forLoop() { var i=0 for (i=0;i<=3;i++) { alert(i) } } </SCRIPT> </HEAD> <BODY> <FORM name=form1> <INPUT id=btn1 onclick="forLoop()" type=button value=Enter> </FORM> </BODY> </HTML>

The while loop:

Use the while loop when you want your code to continue executing as long as the specified condition is true.

 syntax

while (variable <= end_value)
{
code to be executed;
more code to be executed
;
}


<HTML> <HEAD> <TITLE> Conditional Statements</TITLE> < SCRIPT LANGUAGE="JavaScript"> function whileLoop2() { var i=0 while (i<=3) { alert(i) i=i+1 } } </SCRIPT> </HEAD> <BODY> <FORM name=form2> <INPUT id=btn1 onclick="whileLoop2()" type=button value=Enter> </FORM> </BODY> </HTML>

The do while loop:

Use the do while loop  to run the same block of code while a specified condition is true. This loop will always be executed at least once.

=================

do
{
    code to be executed
}
while (var<=endvalue)

=================

function dowhileloop3()
{
var i=0
do
{
alert(i)
i=i+1
}
while (i<1)
}