← ACARA Digital Technologies Years 7–8

Nested loops: a loop inside a 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.

Understanding Nested loops: a loop inside a loop in ACARA Digital Technologies Years 7–8

What it does: The inner loop builds one full row of 4 stars each time, and the outer loop repeats this for 3 rows, so the inner loop runs all its repeats for every outer repeat. Using a different counter name for the inner loop (j) keeps the two counters from getting mixed up. A multiplication grid multiplies the row number by the column number, so use i * j. Paired symbols must match. Two nested loops have two opening braces, so an extra closing brace is a syntax error.

Common mistakes: 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++). 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 599–610) and word search (Modules 583–613).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
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?
7 times12 times
Matches
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.
Applying
Meanings
Term
nested loop
Meaning
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.
Practice
for (let i = 1; i <= 3; i++) {
Testing
Gaps
for (let = 1; j <= 4; j++) count++;
Check Answer
Spots
2for (let i=0; i<3; i++) {
3 for (let i=0; i<4; i++) {

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Fox' badge — Cunning Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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 ✓
The inner loop does all 4 repeats for each of the 3 outer repeats, so 3 x 4 = 12, not 3 + 4.
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
The inner loop restarts and runs completely each time the outer loop repeats.
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++) { } }
Using a different counter name for the inner loop (j) keeps the two counters from getting mixed up.
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
2 outer repeats x 5 inner repeats = 10 passes through console.log('*').
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 ✓
For every row (outer), the inner loop goes across all the columns, so each row gets a full set of cells.

Match the Pairs

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.
console.log(i * j); → Inside a nested loop, this prints one multiplication result on every pass, using both counters to build a multiplication grid.
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); }
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);
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.
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.

Key Term

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.

Fill in the Blanks

1. for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) count++; }
Hint: Type the counter name for the inner loop, which must differ from the outer counter i.
2. for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) count++; }
Hint: Type the inner loop limit so that an outer loop of 3 gives 12 passes in total.
3. for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) t += i * j; }
Hint: Type the operator that multiplies the two counters to build a multiplication grid.
4. for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 5; j++) row += "*"; }
Hint: Type the shorthand operator that adds one more star onto the row each time the inner loop runs.
5. for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 4; j++) count++; }
Hint: Type the keyword that starts the second loop placed inside the outer loop.

Spot the 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 }
Line 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++).
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 }
Line 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.
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 }
Line A — The parts of a for loop header are separated by semicolons, not commas. It should be for (let i=1; i<=3; i++) {.
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);
Line 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.
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 }
Line B — JavaScript is case sensitive, so For is not the keyword. It must be lowercase: for (let i=0; i<3; i++) {.

Student with a class code? Join your class →