Nested and mixed iteration

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

Nested and mixed iteration

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Can a FOR loop be nested inside a WHILE loop?
A False
B True
2. Does each nested loop need its own matching closing keyword?
A True
B False
3. In mixed iteration, does the inner loop run fully for each repetition of the outer loop?
A False
B True
4. Can a WHILE loop be nested inside a FOR loop?
A True
B False
5. If a FOR loop is nested inside a WHILE loop, should it close with END WHILE?
A False
B True

Match the Pairs

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

Left columnRight column
1WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i END FOR END WHILE ACloses the inner FOR loop first, then the outer WHILE loop
2END FOR followed by END WHILE BFOR j FROM 1 TO 5 WHILE total < 20 DO
3Nesting one loop type inside a different loop type CThe inner WHILE loop must be closed with END WHILE before END FOR
4A WHILE loop written entirely inside the body of a FOR loop DMixed iteration
5WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i ENDFOR END WHILE EA FOR loop nested inside a WHILE loop
6FOR i FROM 1 TO 3 WHILE total < 5 DO total ← total + i END FOR FThe inner loop should close with END FOR, not ENDFOR

Definitions

Mixed iteration

Fill in the Blanks

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

1.
WHILE total < 10 DO ___ i FROM 1 TO 3
2.
WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i _______
3.
WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i END FOR _________
4.
FOR j FROM 1 TO 5 _____ total < 20 DO
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 WHILE total < 10 DO
B FOR i FROM 1 TO 3
C total ← total + i
D END FOR
E END LOOP
Question 2
A FOR j FROM 1 TO 5
B WHILE total < 20 DO
C PRINT total
D total ← total + 1
E END WHILE
F END FOR
Question 3
A WHILE count < 5 DO
B FOR k FROM 1 TO 2
C count ← count + 1
D ENDFOR
E END WHILE
Question 4
A FOR i FROM 1 TO 3
B WHILE total < 15
C total ← total + i
D END WHILE
E END FOR
Question 5
A total ← 0
B WHILE total < 10 DO
C FOR i FROM 1 TO 3
D total = total + i
E END FOR
F END WHILE

Answer Key

Nested and mixed iteration
Section A: Quick Questions
Q1
B — True
Q2
A — True
Q3
B — True
Q4
A — True
Q5
A — False
Section B: Match the Pairs
Q1
WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i END FOR END WHILE → A FOR loop nested inside a WHILE loop
Q2
END FOR followed by END WHILE → Closes the inner FOR loop first, then the outer WHILE loop
Q3
Nesting one loop type inside a different loop type → Mixed iteration
Q4
A WHILE loop written entirely inside the body of a FOR loop → FOR j FROM 1 TO 5 WHILE total < 20 DO
Q5
WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i ENDFOR END WHILE → The inner loop should close with END FOR, not ENDFOR
Q6
FOR i FROM 1 TO 3 WHILE total < 5 DO total ← total + i END FOR → The inner WHILE loop must be closed with END WHILE before END FOR
Section C: Definitions
Q1
Mixed iteration — Nesting one loop type inside a different loop type, such as a FOR loop inside a WHILE loop
Section D: Fill in the Blanks
Q1
WHILE total < 10 DO FOR i FROM 1 TO 3
Q2
WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i END FOR
Q3
WHILE total < 10 DO FOR i FROM 1 TO 3 total ← total + i END FOR END WHILE
Q4
FOR j FROM 1 TO 5 WHILE total < 20 DO
Q5
The inner loop runs fully for each cycle of the outer loop
Section E: Spot the Error
Q1
E — The outer WHILE loop must be closed with END WHILE, not END LOOP.
Q2
C — VCAA pseudocode uses DISPLAY to output a value, not PRINT.
Q3
D — The closing keyword for the inner loop should be written as END FOR, not ENDFOR.
Q4
B — The WHILE condition is missing DO — it should read WHILE total < 15 DO.
Q5
D — Assignment in pseudocode uses ←, not =.