## Please visit my new Web Site WWW.Codedisplay.com

First of all what is loop or iteration? Loop is used for executing a set of statements or instructions repeatedly until a specific condition is false. To perform the repeated task like other programming languages Javascript provides us loop. If we needs to run multiple statement within a loop then the statements must be enclosed within the loop body by using braces { and }. Keep in mind two things when you want to write loop statement are:

1. Statements that you need to iterate.
2. Termination or exit from loop. This is must otherwise infinite loop may hang.

In Javascript there are 3 different type of loops:
1. for loop : Specified number of times.
2. while loop : While condition is true.
3. do-while loop : First time must execute then loops while condition is true.

In this post i also want to discuss on the following topics:
1. BREAK Statement to exit from a loop
2. Inner loop or Nested loop

For loop Syntax & Example:
The basic syntax for the "for loop" statement is given below:
for (Initialization; Condition; Increment/Decrement)
{
...
statements
...
}

Example:

<script type="text/javascript">
// This is the incremental for loop
document.write("***** Start For loop *****</br>");
for(i = 0; i < 5; i++)
{
document.write("Iteration Number = " + i+"</br>");
}
document.write("***** End For loop *****");
</script>

<script type="text/javascript">
// This is the decremental for loop
document.write("</br>***** Start For loop *****</br>");
for(i = 4; i >= 0; i--)
{
document.write("Iteration Number = " + i+"</br>");
}
document.write("***** End For loop *****");
</script>

Display:

***** Start For loop *****
Iteration Number = 0
Iteration Number = 1
Iteration Number = 2
Iteration Number = 3
Iteration Number = 4
***** End For loop *****

***** Start For loop *****
Iteration Number = 4
Iteration Number = 3
Iteration Number = 2
Iteration Number = 1
Iteration Number = 0
***** End For loop *****

While loop Syntax & Example:
The basic syntax for the "while loop" statement is given below:

while (Condition)
{
...
statements
...
}

Example:

<script type="text/javascript">
// This is the incremental for loop
var i=0;
document.write("***** Start While loop *****</br>");
while(i < 5)
{
document.write("Iteration Number = " + i+"</br>");
i++;
}
document.write("***** End While loop *****");
</script>

<script type="text/javascript">
// This is the decremental for loop
var i=4;
document.write("</br></br></br>***** Start While loop *****</br>");
while(i >= 0)
{
document.write("Iteration Number = " + i+"</br>");
i--;
}
document.write("***** End While loop *****");
</script>

Display:
Same as for loop.

Do-While loop Syntax & Example:
Do while loop must perform first time. After that it will check the while loop condition & execute until condition is false. The basic syntax for Do-While loop is given below:

do{
...
statements
...
}while (Condition)

Example:

<script type="text/javascript">
// This is the incremental do while loop
var i=0;
document.write("***** Start Do While loop *****</br>");

do{
document.write("Iteration Number = " + i+"</br>");
i++;
}while(i < 5)
document.write("***** End Do While loop *****");
</script>

<script type="text/javascript">
// This is the decremental do while loop
var i=4;
document.write("</br></br></br>***** Start Do While loop *****</br>");

do{
document.write("Iteration Number = " + i+"</br>");
i--;
}while(i >= 0)
document.write("***** End Do While loop *****");
</script>

Display:
Same as above.

Using BREAK Statement:
Most often we need to exit from a loop when any one condition from a set of terminating condition is meet. In the following example i will show you how we can exit from a "For loop" by using BREAK statement.

Example on BREAK Statement:

<script type="text/javascript">
document.write("</br></br></br>***** Start For loop*****</br>");
for(i = 0; i < 5; i++)
{
document.write("Iteration Number = " + i+"</br>");
// want to exit for loop when i = 3
if(i==3)
break;
}
document.write("***** End For loop *****</br>");
</script>

Display:

***** Start For loop*****
Iteration Number = 0
Iteration Number = 1
Iteration Number = 2
Iteration Number = 3
Exit for loop.....
***** End For loop *****

Nested Loop/Inner Loop:

<script type="text/javascript">
document.write("***** Start For loop for i *****</br>");
for(i = 0; i < 3; i++)
{
document.write("</br>");
document.write("Iteration Number for I = " + i+"</br>");
document.write("***** Start For loop for j while i="+i+" *****</br>");
// This is the nested for loop
for(j=0;j<2;j++)
{
document.write("Iteration Number for j = " + j+"</br>");
}
document.write("***** End For loop for j while i="+i+" *****</br>");
document.write("</br>");
}
document.write("***** End For loop *****</br>");
</script>

Display:

***** Start For loop for i *****

Iteration Number for I = 0
***** Start For loop for j while i=0 *****
Iteration Number for j = 0
Iteration Number for j = 1
***** End For loop for j while i=0 *****

Iteration Number for I = 1
***** Start For loop for j while i=1 *****
Iteration Number for j = 0
Iteration Number for j = 1
***** End For loop for j while i=1 *****

Iteration Number for I = 2
***** Start For loop for j while i=2 *****
Iteration Number for j = 0
Iteration Number for j = 1
***** End For loop for j while i=2 *****

***** End For loop *****

Examine the output for the above example you will found that for each iteration of i, j executed two times.

 Google Search on Internet Subscribe RSS  