EK 4.1.2G Every algorithm can be constructed using only sequencing, selection, and iteration.
EK 5.2.1A Algorithms are implemented using program instructions that are processed during program execution.
EK 5.2.1B Program instructions are executed sequentially.
EK 5.2.1C Program instructions may involve variables that are initialized and updated, read, and written.
Computer scientists describe this program structure as looping, repetition, or iteration. Sequencing, selection, and iteration are building blocks of algorithms. You may wish to look at Unit 1 Lab 2 Page 4: Learning Player Names for a review of sequencing and selection.
forever
block generates an infinite loop that goes on forever. An infinite loop can sometimes be the result of a bug, but in some interactive programs, you want the program to keep running until stopped by the user.You've seen these ways to repeat a set of commands:
On this page, you will use for
loops to repeat and count the repetitions so you can use that counter to draw shapes with repeated patterns...
You can use to name a variable (here, ) that counts each repetition, and you can use that counter variable in the repeating script. For example, the for
block lets you simplify long scripts like:
for
block runs the script inside, it changes the value of the variable by 1, counting from the first input number to the second.for
block isn't installed automatically. You'll need to import tools once into each new snap project where you need for
and some other important tools. for
block gives you a default variable, i (for index). You can change the name of this variable by clicking it. Once changed, drag it out and use it just like any variable.for
block in the squiral script. What is the result?turn
and move
to get as close as you can get to a smooth spiral: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: nest polygons
that accepts the number of polygons and the number of sides for the polygons.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.pinwheel
code with inputs. Find out how to create your own artistic animations.The following code may give you ideas about how to create animations. Make sure to remove all wait
0.5 secs
blocks from your pinwheel
code so they don't slow it down. The warp
block allows the drawing of the pinwheel all at once.
move
, it changes the input to turn
: