JavaScript - Looping Statement

Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true.

Very often when you write code, you want the same block of code to run over and over again in a row.

Instead of adding several, almost equal lines in a script we can use loops to perform a task like this.

In JavaScript there are two different kinds of loops:
          (1)  For
          (2)  While

For Loop :

Loops through a block of code a specified number of times

Syntax:

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

When a for loop executes, the following occurs:

The initializing expression initial-expression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity.

The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the for loop terminates.

The statements execute.

The update expression incrementExpression executes, and control returns to Step 2.

Example
<html>
<body>
<script language=“javascript”>
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">Visions Developer " + i);
document.write("</h" + i + ">");
}
</script>
</body>
</html>

The example above defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 6. i will increase by 1 each time the loop runs. Results in printing six predefined headers in HTML.
 

 While Loop :

Loops through a block of code while a specified condition is true while loop, is used to run the same block of code while a specified condition is true.
Syntax:

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

Example

<html>
<body>
<script language=“javascript”>
var i=0
while (i<=10)
{
document.write("The number is " + i);
document.write("<br />");
i=i+1;
}
</script>
</body>
</html>

The example above defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 10. i will increase by 1 each time the loop runs.

Do….While Loop

The do...while statement repeats until a specified condition evaluates to false.


Syntax:
do
{
statement
} while

Statement executes once before the condition is checked. If condition returns true, the statement executes again.

At the end of every execution, the condition is checked. When the condition returns false, execution stops and control passes to the statement following do...while.

Example

<html>
<body>
<script language=“javascript”>
i=0;
do
{
i++;
document.write(i);
} while (i<5);
</script>
</body>
</html>

In the following example, the do loop iterates at least once and reiterates until i is no longer less than 5.

Break statement
Use the break statement to break the loop.

Continue statement

Use the continue statement to break the current loop and continue with the next value.

For...In Statement

The for...in statement is used to loop (iterate) through the elements of an array or through the properties of an object. The code in the body of the for ... in loop is executed once for each element/property.

Syntax:
for (variable in object)
{
code to be executed
}

Example

<script language=“javascript”>
var x;
var mycars = new Array();
mycar s[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>

Above code will display all the elements of array mycars on web page. With each element on new line