For loop
This is used to repeat certain block of code (statements) in specified number of times.
|
for (expression 1; expression 2; expression 3){ // JavaScript statements }
Expression 1 sets up the counter variable. Expression 2 is the condition, the loop will execute till condition evaluates to true. Expression 3 is to update the counter variable specified in expression 1.
|
for( var count =1; count<=10; count++){ document.writeln(count); } The following code prints numbers from 1 to 10.
|
While loop
This is used to repeat certain block of code (statements), which until a condition met.
|
while(expression){ // JavaScript statements }
Expression is a condition that evaluates to a Boolean value .The statements will execute as long as the condition is true.
|
var count =1; while(count<=10){ document.writeln(count); count++; } The following code prints numbers from 1 to 10.
|