ST EK List:
1.3.1D Digital effects and animations can be created by using existing software or modified software that includes functionality to implement the effects and animations.
4.1.1A Sequencing, selection, and iteration are building blocks of algorithms.
4.1.1D Iteration is the repetition of part of an algorithm until a condition is met or for a specified number of times.

Looping with a Counter

EK 4.1.2G Every algorithm can be constructed using only sequencing, selection, and iteration.

EK 5.2.1A Algorithms are implemented using program instructions that are processed during program execution.
EK 5.2.1B Program instructions are executed sequentially.
EK 5.2.1C Program instructions may involve variables that are initialized and updated, read, and written.

Computer scientists describe this program structure as looping, repetition, or iteration. Sequencing, selection, and iteration are building blocks of algorithms. You may wish to look at Unit 1 Lab 2 Page 4: Learning Player Names for a review of sequencing and selection.

The forever block generates an infinite loop that goes on forever. An infinite loop can sometimes be the result of a bug, but in some interactive programs, you want the program to keep running until stopped by the user.

You've seen these ways to repeat a set of commands:

On this page, you will use for loops to repeat and count the repetitions so you can use that counter to draw shapes with repeated patterns...
spiral nested squares

You can use for(i)=(1) to (10){} to name a variable (here, i) that counts each repetition, and you can use that counter variable in the repeating script. For example, the for block lets you simplify long scripts like:

Each time the for block runs the script inside, it changes the value of the variable by 1, counting from the first input number to the second.
say (1) for (2) secs, say (2) for (2) secs, say (3) for (2) secs... say (10) for (2) secs       to for (i)= (1) to (10) {say (i) for (2) secs}

Import Tools

In the current version of snap, the for block isn't installed automatically. You'll need to import tools once into each new snap project where you need for and some other important tools.
select 'Import tools' from the Snap! file menu
  1. Click on the File File menu icon menu at the top of the snap window and select "Import Tools."
  2. The for block gives you a default variable, i (for index). You can change the name of this variable by clicking it. Once changed, drag it out and use it just like any variable.
  3. Build this script that makes the sprite say the numbers 1 through 10.
    for (i) = (1) to (10) {say (i) for (2) secs}
    1. Then modify it so that the sprite says 0, 2, 4, 6, 8, ... up through 30.
    2. Discuss your solutions with another pair.
  4. Experiment with spirals.
    1. Build this script and try it out:
      This design got the nickname "squiral" because it's a square spiral.
      pen down; for(length)=(1) to (100){move(2Xlength) steps; turn clockwise (90) degrees}
    2. Talk with Your Partner Make sure you can explain why the squiral spirals outward.
    3. Try switching the order of the 100 and the 1 in the for block in the squiral script. What is the result?
    4. Try changing the turning angle in the squiral script to other numbers such as 92, 126, etc.
    5. Change the inputs to turn and move to get as close as you can get to a smooth spiral:
      spiral
  5. "U1L3-Squiral"Save your work as U1L3-Squiral
  1. Open your U1L3-Pinwheel project, and build a nest squares block that uses for and your polygon block to draw nested squares. Give it an input so that it will draw whatever number of squares you specify, with each square larger than the previous:
    nested squares
  2. Build nest polygons that accepts the number of polygons and the number of sides for the polygons.
  3. Build a script that draws 12 regular polygons, each with one more side than the previous one, as shown below.
    polygons: triangle through decagon
  4. Predict what this script will do before you try it:
    for (tens) = (0) to (9){ for (ones) = (0) to (9){ say (join (tens) (ones)) for (0.3) secs}}
 
  1. Build a script that counts down by 10 from 100 to 0 (that is, 100, 90, 80, etc.).
  2. Find a way to use for to nest squares this way. Build your block with two inputs that let you specify how many squares the design will contain and how much bigger each square will be than the previous one.
    Tough Stuff concentric squares
  3. Below are two animations that use the pinwheel code with inputs. Find out how to create your own artistic animations.
    Array of pinwheels animation Pinwheel wreath animation

    The following code may give you ideas about how to create animations. Make sure to remove all wait 0.5 secs blocks from your pinwheel code so they don't slow it down. The warp block allows the drawing of the pinwheel all at once.

    forever{for(i)=(0) to (50){clear;warp{pinwheel, branches:(12) size:(50) backup:(i)}}}
  4. Tough Stuff Tough Stuff Tough Stuff This block is like the squiral, but instead of changing the input to move, it changes the input to turn:
    inspi repeat:(num) size:(size) angle:(angle) : for (i) = (1) to (num) {move (size) steps; turn ((i) * (angle)) degrees}
    1. Try sketching what it will draw with an angle of 2.
    2. Then build it, and try each of these tests:
      You can stop each test with the stop button (stop button) when you're sure nothing new will happen, but don't decide that too quickly!
      inspi with (1000,10,80), (1000,5,1), (1000,5,7), (1000,5,13), (1000,10,77)
    3. What's going on? Can you work out a theory to predict anything about the shape it draws for a particular angle input?
      Hint: Think about divisibility.