Trying it out - Spiraling Around: The for Loop

Objective

After completing this lesson, you will be able to apply the concepts of the previous lesson - Spiraling Around: The for loop.

Exercise

Your Turn!

Now it’s your turn. Download this document for a recap of the blocks we covered in this lesson and some hands-on exercises for you to explore.

What You Have Learned in This Lesson

Variables

In this lesson, you learned about variables. A variable is a placeholder or a container in which you can store values like numbers, texts or even lists or blocks.

You can create variables in the "Variables" category by clicking on the "Make a variable" button. You can give the variable a name of your choice.

Additionally, you can decide between a global variable which is available for all sprites or a sprite-local variable ("for this sprite only") only accessible from your current sprite. Clicking the OK button generates the variable.

You’ll get the option to select this variable in the dropdown menu of the set variableblock (1) to store values during your program. You can access these values with the variable reporter (2) or change them again with the change variable block (3). You can display the current variable value on the stage by checking the checkbox next to the variable reporter in the palette.

The variable creation process. Clicking on the “Make a variable” button in the “Variables” category will open the Variable dialog, where name and scope of the variable can be selected. Pressing OK will generate the variable and create a variable reporter. It also makes this variable accessible in the dropdown menu of the change and set variable blocks.

The for-loop

The for- loop is the second type of loop, that you got to know in Snap!

Like the repeat-loop the for-loop reruns the enclosed blocks a specific number of times, but this time with a twist. When using the for -loop, you must enter a start and end value for its internal variable i. In each iteration i is automatically incremented by 1 until the end value is reached. When i is equal or greater than the end value, the loop stops.

You can drag the internal variable i out of the block and use it in your script and use it to let your object say a countdown or draw beautiful spirals.

the for-loop has an internal variable i, a start and an end value. The variable i can be referenced in the enclosed blocks.