The For Block

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 and some other important tools.

You have already used three ways to repeat a set of actions.

  1. The the forever block block tells the computer to repeat the same action forever.
  2. The the repeat block block lets you repeat an action a specific number of times.
  3. The repeat until block lets you repeat an action until some special condition occurs.
On this page, you will learn a fourth useful way.
  1. The for block lets you name a variable (here, i) that says which repetition you are on. You can use the variable in the script you are repeating.

The for block lets you simplify long scripts like:

Each time the for block runs your script, it changes the value of the variable by 1, counting from the first input number to the second. You can use that variable in your script.
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 (Hello!) for (2) secs

  1. The script above makes the sprite say the numbers 1 through 10. Build it and modify it so that the sprite says 0, 2, 4, 6, 8, ... up through 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's a square spiral.
  4. Talk with Your Partner Make sure you can explain 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. Build a block nest squares that uses for and your square block to nest 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
  9. "U1L3-Squiral"Save your work as U1L3-Squiral
 
  1. Build a script that counts by 10 from 100 to 0 (that is, 100, 90, 80, etc.).
  2. Tough Stuff Build a block with inputs that let you specify how to count. If you say count-by-10-from-100-to-0, it counts 100, 90, 80, etc., but if you say count-by-2-from-0-to-30, it counts 0, 2, 4, 6, ..., 28, 30.
  3. Tough Stuff Find a way to use for to nest squares this way.
    concentric squares

    Build a 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.

  4. Tough Stuff Build a script that draws 12 regular polygons, each with one more side than the previous one.
  5. Build a script that counts from 3.14 to 0.14 by hundredths and then counts back again.
  6. 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