What is nested loop…
 
Notifications
Clear all

[Solved] What is nested loops?

5 Posts
5 Users
5 Likes
581 Views
1
Topic starter

Please reply asap!!!!

This topic was modified 2 years ago by sarva
demo.learner demo.learner 04/03/2023 5:16 pm

@sarva Please post answers as well on other’s questions.

sarva sarva Topic starter 05/03/2023 6:12 am

@demo.learner sure

Rivek.t Rivek.t 05/03/2023 7:34 am
This post was modified 2 years ago 2 times by Rivek.t

@sarva Nested loops are loops within loops, where the inner loop is executed multiple times for each iteration of the outer loop.

 

Here’s an example of nested loops in JavaScript that prints out the multiplication table from 1 to 10:

 

for (var i = 1; i <= 10; i++) {  // Outer loop for rows
  for (var j = 1; j <= 10; j++) { // Inner loop for columns
    console.log(i * j); // Print the product of i and j
  }
  console.log("\n"); // Add a new line after each row
}
2 Answers
3

A nested loop means a loop statement inside another loop statement. That is why nested loops are also called “loop inside loops“. We can define any number of loops inside another loop.

1. Nested for Loop

Nested for loop refers to any type of loop that is defined inside a ‘for’ loop.

Syntax:

for ( initialization; condition; increment ) {

   for ( initialization; condition; increment ) {
      
      // statement of inside loop
   }

   // statement of outer loop
}


2. Nested while Loop

A nested while loop refers to any type of loop that is defined inside a ‘while’ loop.

Syntax:

while(condition) {

   while(condition) {
      
      // statement of inside loop
   }

   // statement of outer loop
}
1

A nested loop refers to the process of running a loop within a loop. That can be a nested for loop or a nested while loop.

Share: