Nested Loop in JavaScript

Nested Loop in JavaScript present inside the different loop. Javascript helps the nested loop in javascript. The loop can hold more than one number of loops set inside another loop, and also can perform n level of nesting inside the loop. The nested loop is also called as inner loop and the loop in which the nested loop is determined is an outer loop.

The outer loop continuously runs first and the inner loop performs, the inner loop performs per time the outer loop performs once. In the state of multi-level nested an outer loop runs first and then the 1st inner loop executes and then the 2nd inner loop executes and so on. Any type of nested loop can be specified inside any type of loop.

Syntax Nested Loop in JavaScript

Outerloop
{
Innerloop
{
// statement to be run inside inner loop
}
// statement to be run inside outer loop
}

The Outer loops and Inner loop are the loops that can be any type of loop-like for loop in javascript, do-while loop, or while loop in javascript.

Flowchart for Nested for Loop:)

Following, we will see the flow chart for Nested Loop:

Nested Loop in JavaScript

Note to the above-nested chart: An above picture presents the flow of execution in the nested for loop, as in the image we can definitely view that the outer loop first assesses, if it is true then assesses the nested loop, and then performs its body. After the execution of the inner body repeat, it goes back to the outer loop analysis it and then evaluates the inner loop and so it iterates until they evaluate to true.

Examples to Implement Nested Loop in JavaScript

The nested for the do-while loop means any type of loop that is specified inside the do-while loop:

Example:

<script>
// Nested Loop Example
for (num = 0; num < 5; num++) { // numbers from 1 to 5 times
    document.write("<br>"+ "Count from ");
    for (num1 = 1; num1 < 5; num1++){
        document.write(num1);
    }
}
</script>

The sample code above executes the outer loop and the inner loop five times each. The program prints the numbers 1,2-3 4 several times. The inner loop will display the specific digits while the outer loop defines how many times this job is iterated. Try modifying num< 5 to num< 0. The numbers 0 through 5 should only be displayed once if this modification is made. This is because the outer loop is only performed once. If the break; statement lived in the inner loop, it will only stop the inner loop, not the outer loop.

Example:)

The nested for loop implies any type of loop that is defined inside the for loop:

<button onclick="myFunc()">Click me
</button>
<p id="num"></p>
<script>
function myFunc() {
var x=" ";
var y = [
[5, 10],
[20, 30],
[40, 50] ];
var i=0;
while (i < 3) {
var j=0;
while (j < 2 ) {
var sum=0;
sum=y[i][j]+y[i][j];
x += "The sum of a+a, at i = " + i + " and j = " + j + " is "+sum+" <br>";
j++;
}
i++;
}
document.getElementById("num").innerHTML =
x;
}
</script>

Note to the above program: As in the program code the variable I initialize to 0. We remember that the do-while loop in javascript performs and then checks the condition, which means the inner loop is performed and then stays the outer loop condition. Once the inner loop runs, the program control passes to the increment of the i++ and again the condition is held, if it is true, then the inner loop is executed repeatedly and these steps will be repeated until the condition in the outer loop is true.

Conclusion:

JavaScript holds the nested loop feature, where a loop is started inside another loop. A loop can have one or digits and/or n nested levels of loops specified inside another loop. For each outer loop, the inner loop takes to perform. If the break statement is used inside the inner loop it breaks to the inner loop only, not the outer loop. Nested Loop in JavaScript.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top