Lesson 2: Staircases and Borders — Shrinking Programs with REPEAT
Learning objective
Students will spot repeating groups of arrows inside a grid program, rewrite them using a REPEAT n TIMES: [ ... ] loop block, and verify that the long program and the loop program land on the exact same square.
Materials
- 4×4 Staircase Map on the board and student worksheets
Vocabulary
- Pattern Unit (Core): The small group of steps that repeats in the same order.
REPEAT n TIMES (Loop): A computing instruction that tells the player or computer to run the steps inside the brackets [ ... ] exactly n times in a row.
Lesson flow
1. Opening: The Staircase Climb (7 min)
- Draw a 4×4 Staircase Map where the explorer starts at
S (Col 1, Row 1) and climbs diagonal stairs to T (Col 4, Row 4):
Row 4 (Top): [ . ] [ . ] [ . ] [ T ]
Row 3: [ . ] [ . ] [ * ] [ * ]
Row 2: [ . ] [ * ] [ * ] [ . ]
Row 1 (Bottom): [ S ] [ * ] [ . ] [ . ]
Col 1 Col 2 Col 3 Col 4
- Write the long 6-arrow staircase program along the
* squares:
Long Program: → ↑ → ↑ → ↑
- Ask: "Read or tap through that arrow list with me: 'Right, Up, Right, Up, Right, Up!' What 2-arrow pattern repeats over and over, and how many times does it repeat?"
- Pattern:
[ → ↑ ], repeated 3 times!
- Write the compressed version right underneath:
Loop Program:
REPEAT 3 TIMES: [ → ↑ ]
- Count the arrows we had to write: the Long Program wrote 6 arrows, while the Loop Program wrote only 2 arrows inside the box (
6 − 2 = 4 arrows saved!).
2. Main activity: Unrolling & Compressing Loops (18 min)
- Practice Unrolling a Loop (6 min):
- What if a programmer hands you a loop and you want to trace every single step? You unroll it!
- Write on the board:
REPEAT 3 TIMES: [ ↑ ] followed by REPEAT 3 TIMES: [ → ]
- Unroll it together on the 4×4 grid starting at
S (Col 1, Row 1):
- First loop (
REPEAT 3 TIMES: [ ↑ ]) = ↑ ↑ ↑ (climbs straight up the left wall to Col 1, Row 4).
- Second loop (
REPEAT 3 TIMES: [ → ]) = → → → (walks straight across the top row to T at Col 4, Row 4!).
- Pair Loop Compression Puzzles — Map-Free Symbolic Pattern Puzzles (12 min):
Explain that the next three puzzles are map-free symbolic pattern puzzles (we are compressing the repeating symbol sequence itself rather than tracing from
S on the 4×4 Staircase Map above; students can write, point to, or dictate their answers, and for Puzzle A may tap/point/say the rhythm instead of clapping or jumping):
- Puzzle A (Rhythm Pattern — Clap/Tap/Point):
Clap Clap Jump | Clap Clap Jump | Clap Clap Jump | Clap Clap Jump
- Answer:
REPEAT 4 TIMES: [ Clap Clap Jump ] (12 actions shrunk to 1 loop of 3 actions!).
- Puzzle B (Symbolic Zig-Zag Arrow Sequence):
↑ ← ↑ ← ↑ ←
- Answer:
REPEAT 3 TIMES: [ ↑ ← ].
- Puzzle C (Symbolic Loop + Extra Step at the End!):
→ ↑ → ↑ → ↑ → (7 arrows total)
- Guide students to see that the first 6 arrows are
REPEAT 3 TIMES: [ → ↑ ], followed by one extra → outside the loop:
REPEAT 3 TIMES: [ → ↑ ] then →.
3. Discussion questions (5 min)
- Question 1: "Does
REPEAT 3 TIMES: [ → ↑ ] do the exact same thing as REPEAT 3 TIMES: [ → ] then REPEAT 3 TIMES: [ ↑ ]?"
- Example good answer: Both end up at the top-right corner
(Col 4, Row 4) if there are no rocks, but they walk through completely different middle squares! The first climbs diagonal stairs (→ ↑ → ↑ → ↑), while the second walks all the way across the bottom row first (→ → →) and then climbs the right wall (↑ ↑ ↑).
- Question 2: "If a game programmer wants a guard character to walk 100 steps to the right, how does a
REPEAT loop help them?"
- Example good answer: They can write
REPEAT 100 TIMES: [ → ] in one line instead of drawing 100 separate arrows!
4. Closing check (5 min)
- (Map-free symbolic unroll) Unroll this loop into a plain list of arrows:
REPEAT 4 TIMES: [ ↓ → ]. How many total single-square steps does the unrolled list contain?
- (Map-free symbolic compression) Compress this 8-arrow sequence using a
REPEAT loop:
↑ ↑ → ← ↑ ↑ → ←
- (4×4 grid trace) Start at
(Col 1, Row 1) on an open 4×4 grid. Where do you land after running REPEAT 2 TIMES: [ ↑ → ]?
Worked answers for closing check
- Unrolled:
↓ → ↓ → ↓ → ↓ →. Since there are 2 arrows inside the box repeated 4 times, the token takes 4 × 2 = 8 total steps.
REPEAT 2 TIMES: [ ↑ ↑ → ← ] (the 4-arrow pattern ↑ ↑ → ← repeats twice).
- Round 1 (
↑ →) moves from (Col 1, Row 1) → (Col 1, Row 2) → (Col 2, Row 2). Round 2 (↑ →) moves to (Col 2, Row 3) → (Col 3, Row 3).
Differentiation
- Extra support: Have students draw vertical separator bars
| between repeating groups (→ ↑ | → ↑ | → ↑) before counting how many groups there are.
- Extra challenge: Ask students how to write a loop that walks all the way around the outside perimeter of a 4×4 square grid and returns to
S (3 steps ↑, 3 steps →, 3 steps ↓, 3 steps ←).