7️⃣

5.7 Nested Loops

5.7 Nested Loops

A nested loop where a loop is placed inside of another loop. The "inner loop" will be executed for very single iteration of the "outer loop".

Common Example

let activities = [ ['Work', 9], ['Eat', 1], ['Commute', 2], ['Play Game', 1], ['Sleep', 7] ]; for (let i = 0; i < 5; i++) { for (let j = 0; j < 5; j++) { console.log(activities[i][j]); } } /* Output: Work 9 Eat 1 Commute 2 Play Game 1 Sleep 7 */
Here the nested loop iterates through all the values in the 2D array. Then all the values are printed out to the console.
It is typical to use a nested loop to iterate through a 2D array.

Practice

You can add practice problems here if you want. Follow the format below.

Previous Section

6️⃣
5.6 2D Arrays
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.