Nested iteration: a loop inside a loop

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

Nested iteration: a loop inside a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A FOR loop with counter Row runs 3 times and contains a FOR loop with counter Col that runs 4 times. How many times does the inner loop body run in total?
A 7 times (3 + 4)
B 12 times (3 × 4)
2. Which statement describes what happens in nested loops?
A The outer loop completes all its repeats for every single repeat of the inner loop
B The inner loop completes all its repeats for every single repeat of the outer loop
3. Which nested FOR loops are written correctly?
A FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row, Col NEXT Col NEXT Row
B FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row, Col NEXT Row NEXT Col
4. Which pair of counter names is a correct choice for a FOR loop nested inside another FOR loop?
A Outer loop uses Row, inner loop uses Col
B Outer loop uses Count, inner loop also uses Count
5. An outer FOR loop runs 5 times. Inside it is an inner FOR loop that runs 2 times. How many times does the outer loop run in total?
A 5 times
B 10 times

Match the Pairs

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

Left columnRight column
1FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row * Col NEXT Col NEXT Row AFOR Player <- 1 TO 5 WHILE Score < 10 DO Score <- Score + 1 ENDWHILE NEXT Player
2NEXT Col NEXT Row BThe inner and outer loops use the same counter name A, so the inner loop changes the outer counter. Use different names such as A and B.
3The inner loop restarts and completes all its repeats every time the outer loop repeats once CA FOR loop inside a FOR loop: the inner body runs 3 x 4 = 12 times in total
4A WHILE loop can be nested inside a FOR loop, and the WHILE loop finishes before the next FOR repeat DCloses the inner loop first, then closes the outer loop
5FOR A <- 1 TO 3 FOR A <- 1 TO 2 OUTPUT A NEXT A NEXT A EThe NEXT statements are in the wrong order. The inner loop must end first: NEXT Y then NEXT X.
6FOR X <- 1 TO 3 FOR Y <- 1 TO 2 OUTPUT X, Y NEXT X NEXT Y FFOR Day <- 1 TO 2 FOR Lap <- 1 TO 3 OUTPUT Day, Lap NEXT Lap NEXT Day

Definitions

nested loop

Fill in the Blanks

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

1.
FOR Row ← 1 TO 3 ___ Col ← 1 TO 4 OUTPUT Row, Col
2.
OUTPUT Row, Col NEXT ___ NEXT Row
3.
FOR Day ← 1 TO 5 FOR Lap ← 1 TO _ OUTPUT "Run" // runs 15 times
4.
FOR Round ← 1 TO 3 _____ Score < 10 DO Score ← Score + 1
5.
WHILE Again = "Y" DO ______ INPUT Mark

Spot the Error

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

Question 1
A FOR Row <- 1 TO 3
B FOR Row <- 1 TO 4
C OUTPUT "*"
D NEXT Col
E NEXT Row
Question 2
A FOR Table <- 1 TO 3
B OUTPUT "Table ", Table
C FOR Num = 1 TO 5
D OUTPUT Num
E NEXT Num
F NEXT Table
Question 3
A FOR Row <- 1 UNTIL 3
B FOR Col <- 1 TO 4
C OUTPUT Row * Col
D NEXT Col
E NEXT Row
Question 4
A FOR Player <- 1 TO 4
B FOR Round <- 1 TO 3
C OUTPUT Player, Round
D NEXT Player
E NEXT Player
Question 5
A Count <- 0
B FOR Row <- 1 TO 3
C FOR Col <- 4 TO 1
D Count <- Count + 1
E NEXT Col
F NEXT Row

Answer Key

Nested iteration: a loop inside a loop
Section A: Quick Questions
Q1
B — 12 times (3 × 4)
Q2
B — The inner loop completes all its repeats for every single repeat of the outer loop
Q3
A — FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row, Col NEXT Col NEXT Row
Q4
A — Outer loop uses Row, inner loop uses Col
Q5
A — 5 times
Section B: Match the Pairs
Q1
FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 OUTPUT Row * Col NEXT Col NEXT Row → A FOR loop inside a FOR loop: the inner body runs 3 x 4 = 12 times in total
Q2
NEXT Col NEXT Row → Closes the inner loop first, then closes the outer loop
Q3
The inner loop restarts and completes all its repeats every time the outer loop repeats once → FOR Day <- 1 TO 2 FOR Lap <- 1 TO 3 OUTPUT Day, Lap NEXT Lap NEXT Day
Q4
A WHILE loop can be nested inside a FOR loop, and the WHILE loop finishes before the next FOR repeat → FOR Player <- 1 TO 5 WHILE Score < 10 DO Score <- Score + 1 ENDWHILE NEXT Player
Q5
FOR A <- 1 TO 3 FOR A <- 1 TO 2 OUTPUT A NEXT A NEXT A → The inner and outer loops use the same counter name A, so the inner loop changes the outer counter. Use different names such as A and B.
Q6
FOR X <- 1 TO 3 FOR Y <- 1 TO 2 OUTPUT X, Y NEXT X NEXT Y → The NEXT statements are in the wrong order. The inner loop must end first: NEXT Y then NEXT X.
Section C: Definitions
Q1
nested loop — A loop placed inside another loop. The inner loop completes all its repeats for every single repeat of the outer loop, so a 3 by 4 nest runs the inner body 12 times.
Section D: Fill in the Blanks
Q1
FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 OUTPUT Row, Col
Q2
OUTPUT Row, Col NEXT Col NEXT Row
Q3
FOR Day ← 1 TO 5 FOR Lap ← 1 TO 3 OUTPUT "Run" // runs 15 times
Q4
FOR Round ← 1 TO 3 WHILE Score < 10 DO Score ← Score + 1
Q5
WHILE Again = "Y" DO REPEAT INPUT Mark
Section E: Spot the Error
Q1
B — The inner loop reuses the outer counter Row, so the two loops fight over the same variable. The inner loop needs its own counter name: FOR Col <- 1 TO 4.
Q2
C — A FOR loop sets its counter with the assignment arrow <-, not =. The line should be FOR Num <- 1 TO 5.
Q3
A — A FOR loop uses the keyword TO to give the end value, not UNTIL (UNTIL belongs to REPEAT). It should be FOR Row <- 1 TO 3.
Q4
D — The inner loop must be closed first with NEXT Round. Closing Player here crosses the loops over. The line should be NEXT Round, then NEXT Player on the last line.
Q5
C — The start value is bigger than the end value, so the inner loop never runs and Count stays 0. It should be FOR Col <- 1 TO 4, giving 3 x 4 = 12 repeats.