Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 105 - 2625 PortGPt V2.1
P. 105

    NESTED LOOP

                     When a loop is used inside another loop then it is called a nested loop. All the three loops (for, while
                     and do-while) provided by JavaScript can be used as nested loop. Syntax to use nested loop is:

                     Outer loop
                     {
                         Inner loop
                         {
                                   //Statements to be executed in the inner loop
                         }
                                   //Statements to be executed in the outer loop
                     }

                      Program 4: To display a right-angle triangle using stars with the help of nested loop.

                     <HTML>
                     <HEAD>
                     <TITLE> Nested Loop </TITLE>
                     </HEAD>
                     <BODY>
                     <H3> Using Nested Loop</H3>
                     <SCRIPT>
                     var i, j;
                     for(i=1; i <= 5; i++)
                     {
                        for(j=1; j<=i; j++)
                        {

                          document.write("*");
                        }
                        document.write("<br />");
                     }
                     </SCRIPT>
                     </BODY>
                     </HTML>

                                                                                                   Loops in JavaScript  103

| 100 101 102 103 104 105 106 107 108 109 110 |