Wednesday, September 30, 2015

The For Block

Repeating parts of your program is a very common practice in programming. There many ways to do it. You have already used two of them:
  • The block is appropriate when you want to repeat the same behavior forever.
  • The block is appropriate when you want to repeat the same behavior a specific number of times.
  • The for block (see Import Tools at right) is appropriate when you want to repeat almost the same behavior with a change in each step.
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 or other advanced blocks.
The numbers in the for block work as you might expect: the for block counts by 1 from the first number to the last number, running the script inside the C-shape with the new number each time. As the for block counts, it keeps track of the changing number with a variable, the orange oval set to i by default, which you can drag into the blocks inside the for loop.
With for, long scripts like:

can be simplfied to:


  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.Talk with Another Pair
  2. 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.
  3. Build this script and try it out:
    squiral script
    This shape is called a "squiral" — a square spiral.
  4. The steps moved varies between repetitions.
  5. Talk with Your Partner Do you see why it spirals outward?
  6. Try switching the order of the 100 and the 1 in the for block in the squiral script. What is the result?
  7. Experiment! Try changing the turning angle in the squiral script to other numbers such as 92, 126, etc.
  8. Change the turn angle and the move steps to get as close as you can get to a smooth spiral:
    spiral
  9. "U1L2-Squiral"Save your work as U1L2-Squiral
  10. Click the script below to lanch it in Snap!:
    when (up arrow) key pressed (for (i = 60 to 84) (say (i), play note (i) for (0.2) beats))
  11. Create a second script that says and plays the notes down from 84 to 60 when you press the down arrow.
  12. You can additional build scripts by dragging more blocks out from the palettes on the left and snapping them together or by duplicating an existing script (by two-finger-clicking or left-clicking it).
  13. Code a script that says and plays the notes from 60 to 83 and then from 84 to 61 a total of 3 times quickly.

 Take IT Further

 Build a script that counts backwards from 100 to 0 by 10s (that is, 100, 90, 80, etc.).
  1. Build a script that nests squares. Draw 10 squares with each square larger than the previous:
    nested squares
  2. Buid a script that draws 12 regular polygons, each with one more side than the previous.
  3. Build a script that counts from 3.14 to 0.14 and then back again.

No comments:

Post a Comment