Home > DeveloperSection > Articles > Loops in Java Script

Loops in Java Script

Posted by  Awadhendra Tiwari
on    March-15-2011 21:09 PM

JavaScript JavaScript 
Ratings:
0 Comment(s)
 4104  View(s)
Rate this:

Loops in Java Script

We can use concept of loops when we want to repeat certain task again and again. We can use concept of loops to reduce line of code and make program more readable and presentable.

In java script there is two types of loop. One is fixed loop in which number of repeat ion is known. Such type of loop are implemented by for keyword another one is   known as variable loop in which number of repeat ion is unknown. Or loop iterates certain step of task until an appropriate action evaluates to true. Such type of loop is implemented by using while keyword. In this demonstration firstly we learn how to implement for loop then in further section we learn how to implement while loop.

for  loop in java script

Syntax of for loop in java script

for (<initialization expression>;<Boolean expression>;<change expression>)
{
            Statements to be executed.
}

Examples which represent for loop

In this example I will show you how to print a table of number by using for loop in java script.

<script type="text/javascript">

        function printTable() {

            var num1 = 5, num2 = 10;

            document.write("Print number between 5 to 10. <br /><br />");

            for (i = num1; i <= num2; i++) {

                for (j = 1; j <= 10; j++) {

                    document.write((j * i) + "&nbsp;&nbsp;");

                }

                document.write("<br /><br />");

            }

        }

</script>

Loops in Java Script

while loop in java script

Syntax for while loop

while(<boolean expression>)
{
                stetements to be executed.
}

Example which represent use of while statement

<script type="text/javascript">

        function printMessage() {

            document.write("while loop demo. <br />");

            var count=0;

            document.write("<font face='Lucida' size='5' color='blue'><ol type='i'>");

            while(count < 10)

            {

                document.write("<li> Number of repeation in while loop is  :"+(count+1));

                count++;

            }

            document.write("</ol></font>");

        }

</script>

Output of the following code snippet is as follows

Loops in Java Script

Using break statements in loop

We can use break statement in loops to break loop at certain situation without completing their life cycle. Break is a keyword which is used to create a break statement.

The following example demonstrate use of break statements in loop

<script type="text/javascript">

        function printMessage() {

            document.write("while loop demo. <br />");

            var count=0;

            document.write("<font face='Lucida' size='4' color='Black'><ol type='i'>");

            while(count < 10)

            {

                document.write("<li> Number of repeation in while loop is  :"+(count+1));

                count++;

                if (count == 5) {

                    document.write("<br /> Loop is going to be break. <br />");

                    break;

                }

            }

            document.write("</ol></font>");

        }

</script>

Output of the following code snippet is as follows

Loops in Java Script

for…. in loop in java script

We can use for….in loop in java script when we want to retrieve some values from any collection like array or property where the number of repletion is not known. Then we use concept of for…in loop. In this demonstration I will tell you that how to implement for…in loop in java script.

Following example demonstrate use of for…in loop in java script

function forInLoop() {

            document.write("This example demonstrate use of for...in loop in java script<br />");

            var nameArr = new Array("Ashish", "Amit", "Awadhendra", "Piyush", "Mark", "John", "David");

            document.write("Displaying content of nameArr <br />");

            for (name in nameArr) {

                document.write("Name you have entered  :&nbsp;" + nameArr[name] + "<br />");

            }

}

In this example I had used new Array () method to create an Array object. And for retrieving values from that array I had written nameArr[name].

Output of the following code snippet is as follows

Loops in Java Script
















Don't want to miss updates? Please click the below button!