Home > DeveloperSection > Blogs > Using JavaScript Nested Loops

Using JavaScript Nested Loops


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

Using JavaScript Nested Loops

Introduction

In this blog I am going to explain the use of nested for loops in JavaScript.

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Nested loops in JavaScript</title>

</head>

<body>

    <script type="text/javascript">

        var i, j, mulTot;

        for (i = 1; i <= 15; i++) {

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

                mulTot = i * j;

 

                if (mulTot < 10) {

                    mulTot = " " + mulTot;

                }

                else {

                    mulTot = " " + mulTot;

                }

                document.write(mulTot);

            }

            document.write("<BR>");

        }

    </script>

</body>

</html>

Output:

Nested loops are used in most of the places it is used in Matrix multiplication,  Displaying tables and many other places. In this example first the outer loop is initialized and if the condition  is true it will give control to the inner loop and this inner loop will keep on incrementing till the condition of the inner loop is true and when the condition of the inner loop becomes false then only the control will be transferred to the outer loop.


Conclusion:

Through this blog we came to know about how to use multiple loops in our program.


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

Follow MindStick