Teacher's Choice

Nesting Repeats (Teacher's Choice)

It is also possible to nest one repeat block inside another. Since snap processes scripts from top to bottom, the first repeat will begin first, and the second repeat will have to complete all its repetitions before the first repeat continues.
repeat (3) (set pen color to (red), move (100) steps, repeat (3) (set pen color to (blue), move (100) steps, turn right (120) degrees), turn right (120) degrees)

  1. Build a script that draws a red triangle, duplicate it twice, and make the two new scripts draw a smaller blue triangle and an even smaller green one.
    • To duplicate a script, right-click (or control-click) on the first block of the script (in this case, the repeat block) and choose "duplicate".
    • Then, change the inputs as the video shows:
      duplicating a script
  2. Try each script by itself to be certain what it does. Then clear the stage.
  3. Now, insert the entire script for the blue triangle between the move and turn blocks of the red triangle, like this.
    embedding the blue triangle in the red triangle Pair Programming Swap
  4. Talk with Your Partner Predict what will happen when you run this script.
  5. Then, try this script to see what it does.
  6. Using the scripts you have, find a way to make this picture.
    Red triangle with blue children and green grandchildren
 
  1. If you like, embed one more triangle, half the size of the last one, in the same way.
  1. This animation shows another example of embedding one script inside a similar script. (You'll make predictions first, so don't build it yet.)
    embedding scripts for squares
    1. Before building this script, sketch what each of the two individual scripts will draw before they are combined.
    2. Predict the number of blue shapes that the combined script will draw.
    3. Sketch the shape that the combined script will draw.
    4. Now, build the combined script and try it out.
    5. Experiment: Embed another square, half the size of the last, in the same way. How many of these smallest squares does the script draw? How could you have predicted that number before trying it?
    6. Predict then experiment: Next, embed one more square, half the size of the last, in exactly the same way. But first, try to predict the number of tiny squares it will draw before trying it.