Are your young coders ready to tackle one of the most powerful concepts in programming? Today, we are taking the Repeat block to the next level by creating a nested loop in ScratchJr. A nested loop is simply a loop (or Repeat block) placed inside another loop—a technique used by professional programmers to make code highly efficient!
The project we’ll use to master this skill is Kitten dancing with friends on a stage. This is a brilliant way to see how one small block can make a character perform a complex routine over and over again.
Setting the Stage for the Routine
To start our programme, we need our backdrop and dancers in place:
- Change the Background: Choose the theatre background so our characters have a stage to perform on. [00:21]
- Add Friends: Add a couple of other characters to join Kitten—perhaps the Penguin and Tack. [00:33]
- Position Dancers: Move all the characters to the front of the stage.
Ensure you have selected the Kitten icon on the left, as we will be building the dance routine for Kitten. [00:53]
Building the Dance Routine: Two Layers of Repetition
A nested loop allows us to define a small, repetitive action (like a shimmy) and then make that entire action repeat as part of a longer routine.
H2. Step 1: Defining the Inner Loop (The “Shimmy” Move)
The inner loop defines the short, repetitive part of the dance—the “shimmy.”
- Create the Shimmy: Go to the blue Motion category and use the Turn blocks to make Kitten rotate slightly left and then right. This forms one single “shimmy” movement. [01:36]
- Add the Repeat Block: Go to the orange Control category and place a Repeat block around the two Turn blocks. [01:31]
- Set Repetition: Keep the default setting of 4 repeats. Now, Kitten will shimmy four times.
H2. Step 2: Defining the Outer Loop (The Full Routine)
The outer loop tells the entire sequence—including the shimmy—to repeat multiple times.
- Start and Travel: Begin the script with the Green Flag block. Before the inner loop, add a Move Right block to make Kitten walk to the side of the stage (e.g., set to 5). [01:07]
- The Nested Loop: Grab a second Repeat block from the orange category. This is the outer loop.
- Enclose the Code: Drag the outer Repeat block over all the programme blocks you’ve created so far—the Move Right block, and the inner Repeat block (the shimmy). [02:08]
- Set Outer Repetition: Set the outer Repeat block to repeat 4 times as well. [02:03]
- Finish: Add a Move Left block to bring Kitten back to the starting position and end the script with the red End block.
Witnessing the Power of Nested Loops
When you run the programme, Kitten will walk right, shimmy four times, return to the start, and then repeat this entire sequence four times! This creates a long, complex dance using only a few of your ScratchJr blocks.
Learning to create a nested loop is a major milestone in coding tutorials. It teaches children how to write concise, professional programmes. If you want more structured activities to practice this advanced skill, be sure to explore our curated lessons for young learners. Happy coding!