Notifications
Clear all
[Solved] What is nested loops?
Students’s Forum
5
Posts
5
Users
5
Likes
581
Views
@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 }
Add a comment
2 Answers
3
04/03/2023 5:14 pm
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 }
Add a comment
Add a comment
1
05/03/2023 8:03 am
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.
Add a comment
Add a comment
Forum Information
- 14 Forums
- 1,836 Topics
- 5,052 Posts
- 0 Online
- 1,078 Members
Our newest member: Richardnop
Latest Post: loli
Forum Icons:
Forum contains no unread posts
Forum contains unread posts
Topic Icons:
Not Replied
Replied
Active
Hot
Sticky
Unapproved
Solved
Private
Closed