A loop inside a loop

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

A loop inside a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An outer REPEAT 3 TIMES has an inner REPEAT 4 TIMES. How many times does the inner step run in total?
A 12 times
B 7 times
2. Which statement describes a nested loop correctly?
A A REPEAT placed inside another REPEAT
B Two REPEATs written one after the other
3. Each time the outer loop repeats once, what does the inner loop do?
A It runs only once
B It runs all of its repeats again
4. Which layout shows both loops indented correctly? A: REPEAT 2 TIMES, then the inner REPEAT indented, then its step indented twice B: REPEAT 2 TIMES, then the inner REPEAT, then its step, all at the left edge
A A
B B
5. How many END REPEAT lines does an algorithm with two nested loops need?
A One, shared by both loops
B Two, one for each REPEAT

Match the Pairs

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

Left columnRight column
1REPEAT 3 TIMES REPEAT 4 TIMES SAY "Hi" ANothing is indented, so it is hard to see that the inner loop sits inside the outer loop. Both loops should be indented.
2REPEAT 2 TIMES REPEAT 5 TIMES SAY "Go" BREPEAT 4 TIMES REPEAT 3 TIMES CHECK SEAT END REPEAT END REPEAT
3Both loops are indented so you can see which loop sits inside the other, and each loop is closed with its own END REPEAT. CREPEAT 3 TIMES REPEAT 2 TIMES SAY "Step" END REPEAT END REPEAT
4The inner loop is a complete REPEAT that starts over for each outer repeat, like 4 rows of 3 seats gives 12 seat checks. DThere is only one END REPEAT for two loops. Each loop needs its own END REPEAT, so a second one is missing.
5REPEAT 3 TIMES REPEAT 4 TIMES SAY "Hi" END REPEAT EThe inner loop runs 5 times for each of the 2 outer repeats, so Go is said 10 times.
6REPEAT 3 TIMES REPEAT 4 TIMES SAY "Hi" END REPEAT END REPEAT FThe inner loop runs 4 times for each of the 3 outer repeats, so Hi is said 12 times.

Definitions

nested loop

Fill in the Blanks

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

1.
REPEAT 3 TIMES ______ 4 TIMES PRINT "*"
2.
SET outer ← 3 SET inner ← 4 SET passes ← outer _ inner
3.
PRINT "*" __________ END REPEAT
4.
REPEAT 2 TIMES REPEAT _ TIMES PRINT "ping" // 10 pings in all
5.
REPEAT _ TIMES REPEAT 4 TIMES PRINT "x" // 12 inner passes in all

Spot the Error

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

Question 1
A REPEAT 3
B REPEAT 4 TIMES
C OUTPUT "*"
D END REPEAT
E END REPEAT
Question 2
A REPEAT 3 TIMES
B REPEAT TIMES 4
C ADD 1 TO total
D END REPEAT
E END REPEAT
Question 3
A REPEAT 2 TIMES
B REPEAT UNTIL 5 TIMES
C OUTPUT "x"
D END REPEAT
E END REPEAT
Question 4
A REPEAT 3 TIMES
B REPEAT 4 TIMES
C OUTPUT "*"
D END IF
E END REPEAT
Question 5
A REPEAT 3 TIMES
B OUTPUT "Row"
C REPEAT 4 STEPS
D OUTPUT "*"
E END REPEAT
F END REPEAT

Answer Key

A loop inside a loop
Section A: Quick Questions
Q1
A — 12 times
Q2
A — A REPEAT placed inside another REPEAT
Q3
B — It runs all of its repeats again
Q4
A — A
Q5
B — Two, one for each REPEAT
Section B: Match the Pairs
Q1
REPEAT 3 TIMES REPEAT 4 TIMES SAY "Hi" → The inner loop runs 4 times for each of the 3 outer repeats, so Hi is said 12 times.
Q2
REPEAT 2 TIMES REPEAT 5 TIMES SAY "Go" → The inner loop runs 5 times for each of the 2 outer repeats, so Go is said 10 times.
Q3
Both loops are indented so you can see which loop sits inside the other, and each loop is closed with its own END REPEAT. → REPEAT 3 TIMES REPEAT 2 TIMES SAY "Step" END REPEAT END REPEAT
Q4
The inner loop is a complete REPEAT that starts over for each outer repeat, like 4 rows of 3 seats gives 12 seat checks. → REPEAT 4 TIMES REPEAT 3 TIMES CHECK SEAT END REPEAT END REPEAT
Q5
REPEAT 3 TIMES REPEAT 4 TIMES SAY "Hi" END REPEAT → There is only one END REPEAT for two loops. Each loop needs its own END REPEAT, so a second one is missing.
Q6
REPEAT 3 TIMES REPEAT 4 TIMES SAY "Hi" END REPEAT END REPEAT → Nothing is indented, so it is hard to see that the inner loop sits inside the outer loop. Both loops should be indented.
Section C: Definitions
Q1
nested loop — A REPEAT placed inside another REPEAT. The inner loop runs all its repeats again for every repeat of the outer loop, so an outer 3 and an inner 4 gives 12 inner passes.
Section D: Fill in the Blanks
Q1
REPEAT 3 TIMES REPEAT 4 TIMES PRINT "*"
Q2
SET outer ← 3 SET inner ← 4 SET passes ← outer * inner
Q3
PRINT "*" END REPEAT END REPEAT
Q4
REPEAT 2 TIMES REPEAT 5 TIMES PRINT "ping" // 10 pings in all
Q5
REPEAT 3 TIMES REPEAT 4 TIMES PRINT "x" // 12 inner passes in all
Section E: Spot the Error
Q1
A — The outer loop must say how many repeats it makes using the word TIMES. It should be 'REPEAT 3 TIMES'.
Q2
B — The inner loop has the words in the wrong order. A counted loop is written REPEAT then the number then TIMES, so it should be ' REPEAT 4 TIMES'.
Q3
B — This mixes two kinds of loop. A set-count loop is written REPEAT 5 TIMES. REPEAT UNTIL needs a condition, not a number of times.
Q4
D — The inner loop is closed with END REPEAT, not END IF. Line 4 should be ' END REPEAT'.
Q5
C — A counted loop uses the keyword TIMES, not STEPS. The inner loop should be ' REPEAT 4 TIMES'.