The For
Block

In the current version of
snap, the
for
block isn't installed automatically. Click on the File

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.
- The
block tells the computer to repeat the same action forever.
- The
block lets you repeat an action a specific number of times.
- The
block lets you repeat an action until some special condition occurs.
On this page, you will learn a fourth useful way.
- The
block lets you name a variable (here,
) 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.
to
- 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.
Discuss your solutions.
-
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.

This design got the nickname "squiral" because it's a square spiral.
Make sure you can explain why the squiral spirals outward.
- Try switching the order of the 100 and the 1 in the
for
block in the squiral script. What is the result?
- Experiment: Try changing the turning angle in the squiral script to other numbers such as 92, 126, etc.
- Change the turn angle and the move steps to get as close as you can get to a smooth spiral:
-
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:
"U1L3-Squiral"
- Build a script that counts by 10 from 100 to 0 (that is, 100, 90, 80, etc.).
Build a block with inputs that let you specify how to count. If you say
, it counts 100, 90, 80, etc., but if you say
, it counts 0, 2, 4, 6, ..., 28, 30.
-
Find a way to use for
to nest squares this way.
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.
Build a script that draws 12 regular polygons, each with one more side than the previous one.
- Build a script that counts from 3.14 to 0.14 by hundredths and then counts back again.
- Predict what this script will do before you try it:
