Nested for loops

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

Nested for loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does a nested FOR loop place one FOR loop entirely inside another?
A True
B False
2. Does the inner loop run fully for every single repetition of the outer loop?
A False
B True
3. Should the inner and outer loops use the same loop variable name?
A False
B True
4. Does each FOR loop in a nested structure need its own END FOR?
A True
B False
5. In a nested loop, which loop must be written first: the inner or the outer?
A Inner
B Outer

Match the Pairs

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

Left columnRight column
1FOR row FROM 1 TO 3 FOR col FROM 1 TO 2 DISPLAY col END FOR END FOR AFOR row FROM 1 TO 3 FOR col FROM 1 TO 2
2Two matching END FOR keywords, one for each loop BA nested FOR loop with an outer and inner loop, each with its own variable
3A loop structure where the inner loop completes fully for each outer pass CRequired to properly close both the inner and outer loops
4Distinct variables, such as row and col, used for the outer and inner loops DThe inner loop should use a different variable name than the outer loop
5FOR row FROM 1 TO 3 FOR row FROM 1 TO 2 DISPLAY row END FOR END FOR EA second END FOR is needed to close the outer loop as well
6FOR i FROM 1 TO 3 FOR j FROM 1 TO 2 DISPLAY j END FOR FNested FOR loop

Definitions

Nested FOR loop

Fill in the Blanks

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

1.
FOR row FROM 1 TO 3 ___ col FROM 1 TO 2
2.
FOR row FROM 1 TO 3 FOR col FROM 1 TO 2 DISPLAY col _______
3.
FOR row FROM 1 TO 3 FOR col FROM 1 TO 2 DISPLAY col END FOR _______
4.
FOR i FROM 1 TO 3 FOR _ FROM 1 TO 2
5.
The inner loop runs fully for each _____ of the outer loop

Spot the Error

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

Question 1
A FOR row FROM 1 TO 3
B FOR row FROM 1 TO 2
C DISPLAY row
D END FOR
E END FOR
Question 2
A FOR i FROM 1 TO 3
B FOR j FROM 1 TO 2
C DISPLAY j
D END FOR
E END WHILE
Question 3
A FOR i FROM 1 TO 2
B FOR j FROM 1 TO 3
C PRINT i, j
D END FOR
E END FOR
Question 4
A FOR i FROM 1 TO 3
B FOR j FROM 1 TO 3
C DISPLAY i
D DISPLAY j
E END FOR
Question 5
A FOR x FROM 1 TO 4
B FOR y FROM 1 TO 2
C total <- total + x
D ENDFOR
E END FOR

Answer Key

Nested for loops
Section A: Quick Questions
Q1
A — True
Q2
B — True
Q3
A — False
Q4
A — True
Q5
B — Outer
Section B: Match the Pairs
Q1
FOR row FROM 1 TO 3 FOR col FROM 1 TO 2 DISPLAY col END FOR END FOR → A nested FOR loop with an outer and inner loop, each with its own variable
Q2
Two matching END FOR keywords, one for each loop → Required to properly close both the inner and outer loops
Q3
A loop structure where the inner loop completes fully for each outer pass → Nested FOR loop
Q4
Distinct variables, such as row and col, used for the outer and inner loops → FOR row FROM 1 TO 3 FOR col FROM 1 TO 2
Q5
FOR row FROM 1 TO 3 FOR row FROM 1 TO 2 DISPLAY row END FOR END FOR → The inner loop should use a different variable name than the outer loop
Q6
FOR i FROM 1 TO 3 FOR j FROM 1 TO 2 DISPLAY j END FOR → A second END FOR is needed to close the outer loop as well
Section C: Definitions
Q1
Nested FOR loop — A FOR loop placed entirely inside another FOR loop, so the inner loop runs fully for every repetition of the outer loop
Section D: Fill in the Blanks
Q1
FOR row FROM 1 TO 3 FOR col FROM 1 TO 2
Q2
FOR row FROM 1 TO 3 FOR col FROM 1 TO 2 DISPLAY col END FOR
Q3
FOR row FROM 1 TO 3 FOR col FROM 1 TO 2 DISPLAY col END FOR END FOR
Q4
FOR i FROM 1 TO 3 FOR j FROM 1 TO 2
Q5
The inner loop runs fully for each cycle of the outer loop
Section E: Spot the Error
Q1
B — The inner loop reuses the outer loop's variable name row — it needs its own distinct variable, such as col.
Q2
E — The outer FOR loop must be closed with END FOR, not END WHILE.
Q3
C — VCAA pseudocode uses DISPLAY to output values, not PRINT.
Q4
E — This closes only one loop — a second END FOR is needed to close the outer loop as well.
Q5
D — The closing keyword should be written as END FOR, not ENDFOR.