Looping with a Counter

On this page, you will use for loops not only to repeat, but also to count the repetitions and then use that counter to draw shapes with repeated patterns:
spiral nested squares

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

: Iteration
AAP-2.J.1

Computer scientists describe a repeating program structure as looping, repetition, or iteration.

AAP-2.K.1

The code can be repeated forever, a specific number of times (such as when using repeat), or until something specific happens (such as when using repeat until as you'll see in Lab 5).
repeat (4) (move (100), turn clockwise (90)) repeat until (touching (Leader)?)
{
    point towards (Leader)
    move (1) steps
}

Sometimes the script inside a loop needs to know which time through the loop it's in (first, second, etc.). You can use for(i)=(1) to (10){} to keep track, and you can use its counter (the default name is i) in the repeated script. For example, the for block lets you simplify long scripts like:
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}

Each time the for block runs the script inside, it changes the value of the counter by 1, beginning with the first input number and ending after the second.

The for block's default counter name, i stands for "index." You can change this name by clicking it. To use the counter, drag it into the script.
  1. 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.
  2. 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
  3. "U1L3-Squiral"Save your work as U1L3-Squiral
  4. 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
  1. Build nest polygons that accepts the number of polygons and the number of sides for the polygons.
  2. Build a script that draws 12 regular polygons, each with one more side than the previous one, as shown below.
    polygons: triangle through decagon
  3. 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}}
  4. Build a script that counts down by 10 from 100 to 0 (that is, 100, 90, 80, etc.).
  5. 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. 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)}}}
  6. mini-Albers-style picture
  7. Make sure your other work has been saved, then Click here to load this file. Then save it to your Snap! account.
    Experiment with the script. Could you change the sizes or colors to make it more interesting?
Albers-style nested squares       Albers-style nested squares       Albers-style nested squares       Albers-style nested squares
  1. 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
painting-by-Josef-Albers

On the right is a painting by Josef Albers. He was interested in experimenting with variations of color within a "family" of similar colors. Snap! will let you make similar experiments.

  1. What is the favorite color of the author of this activity?
  2. If you didn't do the Take It Further activity based on this Kazimir Malevich picture then read it now to learn about Snap! features for solid rectangles and color families.
  3. If you haven't already, do If There Is Time #10, which shows a simple version of this project idea.
  4. Okay, now dig in.
    Randomness: The four examples above are different from the actual Albers picture in that they include occasional colors that aren't from the same family. This can make the pictures more interesting to someone who doesn't share Albers's interest in subtle differences in color. (Albers himself, in fact, had more color variation in other pictures of this type.) Too much randomness, though, can make the picture less interesting. Compare the last two examples above; the left one of those two is clearly based on the orange family, with rainbow-neighbors red and yellow, but the right one has violets, greens, oranges, a yellow—too much chaos, maybe. Look back at the Gossip project if you don't remember how to choose something special once out of six times, or once out of 14 times. Other random choices you can make are the number of squares, how much to shrink the size from one square to the next, and how much to move the corner of the next square horizontally and vertically (the picture is more interesting if those two numbers are different, so the smallest square isn't exactly in the center).
  5. 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.