Nested loops: a loop inside a loop

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Nested loops: a loop inside a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A for loop runs 3 times on the outside and a loop inside it runs 4 times. How many times does the inner code run in total?
A 7 times
B 12 times
2. Which describes what a nested loop does?
A The inner loop finishes all its repeats for every one repeat of the outer loop
B The inner loop runs once, then the outer loop runs once, and they take turns
3. Which is a correct nested loop using the counter names i and j?
A for (let i = 0; i < 3; i++) { for (let j = 0; j < 4; j++) { } }
B for (let i = 0; i < 3; i++) { for (let i = 0; i < 4; i++) { } }
4. An outer loop runs 2 times and an inner loop runs 5 times. The inner loop contains console.log('*'). How many stars are printed?
A 10 stars
B 7 stars
5. You want a multiplication grid with 3 rows and 4 columns. Which loop layout is right?
A Two separate loops one after the other, not nested
B Outer loop for the rows, inner loop for the columns

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) { AInside a nested loop, this prints one multiplication result on every pass, using both counters to build a multiplication grid.
2console.log(i * j); BThe inner loop reuses the counter name i from the outer loop, which makes the code confusing and error-prone. The inner loop should use a different counter name such as j.
3Building a row of stars: the inner loop adds a star to a string for each column, then the outer loop moves on to the next row. CA nested loop: the inner j loop runs 4 times for each of the 3 runs of the outer i loop, giving 12 passes in total.
4An outer loop of 3 repeats with an inner loop of 4 repeats makes the inner loop body run 12 times in total. Dlet count = 0; for (let i = 0; i < 3; i++) { for (let j = 0; j < 4; j++) { count++; } } console.log(count);
5for (let i = 1; i <= 3; i++) { for (let i = 1; i <= 4; i++) { console.log(i); } } Efor (let i = 0; i < 3; i++) { let row = ""; for (let j = 0; j < 4; j++) { row += "*"; } console.log(row); }
6for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) { console.log(i * j); } console.log("-"); } } FThere is an extra closing brace at the end. Each loop needs exactly one matching closing brace, so a nested loop with two opening braces needs only two closing braces.

Definitions

nested loop

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for (let i = 1; i <= 3; i++) { for (let _ = 1; j <= 4; j++) count++; }
2.
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= _; j++) count++; }
3.
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) t += i _ j; }
4.
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 5; j++) row __ "*"; }
5.
for (let i = 1; i <= 3; i++) { ___ (let j = 1; j <= 4; j++) count++; }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let count = 0;
B for (let i=0; i<3; i++) {
C for (let i=0; i<4; i++) {
D count++;
E }
F }
Question 2
A for (let i=0; i<3; i--) {
B let row = '';
C for (let j=0; j<4; j++)
D row += '*';
E console.log(row);
F }
Question 3
A for (let i=1, i<=3, i++) {
B for (let j=1; j<=3; j++) {
C console.log(i * j);
D }
E }
Question 4
A let passes = 0;
B for (let i=0; i<3; i++)
C for (let j=0; j<4; j++)
D passes + 1;
E console.log(passes);
Question 5
A let total = 0;
B For (let i=0; i<3; i++) {
C for (let j=0; j<4; j++) {
D total++;
E }
F }

Answer Key

Nested loops: a loop inside a loop
Section A: Quick Questions
Q1
B — 12 times
Q2
A — The inner loop finishes all its repeats for every one repeat of the outer loop
Q3
A — for (let i = 0; i < 3; i++) { for (let j = 0; j < 4; j++) { } }
Q4
A — 10 stars
Q5
B — Outer loop for the rows, inner loop for the columns
Section B: Match the Pairs
Q1
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) { → A nested loop: the inner j loop runs 4 times for each of the 3 runs of the outer i loop, giving 12 passes in total.
Q2
console.log(i * j); → Inside a nested loop, this prints one multiplication result on every pass, using both counters to build a multiplication grid.
Q3
Building a row of stars: the inner loop adds a star to a string for each column, then the outer loop moves on to the next row. → for (let i = 0; i < 3; i++) { let row = ""; for (let j = 0; j < 4; j++) { row += "*"; } console.log(row); }
Q4
An outer loop of 3 repeats with an inner loop of 4 repeats makes the inner loop body run 12 times in total. → let count = 0; for (let i = 0; i < 3; i++) { for (let j = 0; j < 4; j++) { count++; } } console.log(count);
Q5
for (let i = 1; i <= 3; i++) { for (let i = 1; i <= 4; i++) { console.log(i); } } → The inner loop reuses the counter name i from the outer loop, which makes the code confusing and error-prone. The inner loop should use a different counter name such as j.
Q6
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) { console.log(i * j); } console.log("-"); } } → There is an extra closing brace at the end. Each loop needs exactly one matching closing brace, so a nested loop with two opening braces needs only two closing braces.
Section C: Definitions
Q1
nested loop — A loop inside another loop. The inner loop runs all its repeats for every single repeat of the outer loop, so an outer 3 and inner 4 gives 12 passes.
Section D: Fill in the Blanks
Q1
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) count++; }
Q2
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) count++; }
Q3
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) t += i * j; }
Q4
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 5; j++) row += "*"; }
Q5
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) count++; }
Section E: Spot the Error
Q1
C — The inner loop reuses the counter name i, which clashes with the outer loop's counter. Give it its own name: for (let j=0; j<4; j++).
Q2
A — The outer loop counts down with i-- but the condition i<3 stays true, so it never ends. It should be i++ so the outer loop repeats 3 times.
Q3
A — The parts of a for loop header are separated by semicolons, not commas. It should be for (let i=1; i<=3; i++) {.
Q4
D — passes + 1; works out a value but never saves it, so passes stays 0. Use passes++; so each of the 12 passes adds 1.
Q5
B — JavaScript is case sensitive, so For is not the keyword. It must be lowercase: for (let i=0; i<3; i++) {.