The For Block

Programs often require repeating a set of actions. You have already used three ways and will learn a fourth in this lab session.

The for block lets you simplify long scripts like:

Import Tools

In the current version of snap, the for block isn't installed automatically. Click on the File File menu icon menu at the top of the snap window and select "Import Tools." You'll need to import tools once into each new snap project where you need for.
say (1) for (2) secs, say (2) for (2) secs, say (3) for (2) secs... say (10) for (2) secs
to:
The for block counts by 1 from the first number to the last number, running the script inside it. As the for block counts, it keeps track of the changing number with the variable i. You can use the number in that script. The animation shows how.
for (i) (1) to (10) [say (Hello!) for (2) secs

  1. The example above shows a script that counts from 1 to 10. Build it and modify it so that it counts 0, 2, 4, 6, 8... up to 30.
  2. Talk with Another Pair Discuss your solutions.
  3. Create a new project Build this script and try it out:
    To change the name of the variable from i to length, click on the orange oval without dragging it. After you've changed the name, drag it where you want to use it.
    squiral script
    This design got the nickname "squiral" because it looks like a square spiral.
  4. Talk with Your Partner Do you see why the squiral spirals outward?
  5. Try switching the order of the 100 and the 1 in the for block in the squiral script. What is the result?
  6. Experiment: Try changing the turning angle in the squiral script to other numbers such as 92, 126, etc.
  7. Change the turn angle and the move steps to get as close as you can get to a smooth spiral:
    spiral
  8. "U1L2-Squiral"Save your work as U1L2-Squiral
  9. Click the script below to launch it in snap:
    when (up arrow) key pressed (for (i = 60 to 84) (say (i), play note (i) for (0.2) beats))
  10. Create a second script that says and plays the notes down from 84 to 60 when you press the down arrow.
  11. Because the new script will be so much like what you already have, you may want to duplicate it (right-click or control-click on its hat block) and then change it.
  12. Create a script that says and plays the notes from 60 to 83 and then from 84 to 61 a total of 3 times quickly.
  13. Here are two scripts to build and explore. When you have figured out how they work, build a script that spells your name backwards.

    For i = 1 to 10, say letter i of my-name-is-secret repeat 2 ((for 1=5 to 3, say letter i of abcdefg), wait half 0.5 seconds)

  14. "U1L2-ForLoops"Save your work as U1L2-ForLoops
 
  1. Build a script that counts backwards from 100 to 0 by tens (that is, 100, 90, 80, etc.).
  2. Build a script that nests squares. Draw 10 squares with each square larger than the previous:
    nested squares
  3. Tough StuffNow see if you can make the picture symmetric:
    concentric squares
  4. Build a script that draws 12 regular polygons, each with one more side than the previous one.
  5. Be musically creative: Here are two scripts that may give you ideas.

    repeat 2 ( for i  = 5 to 3 (say letter i of abcdefg, play note ((i x 2) + 54) for 0.5 beats) repeat 2 ( for i  = 3 to 5 (say letter i of abcdefg, play note ((i x 2) + 54) for 0.5 beats), then say letter 3 of abcdefg, play note ((3 x 2) + 54) for 0.5 beats))

  6. Build a script that counts from 3.14 to 0.14 by hundredths and then counts back again.
  7. 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