Using Variables

Using Variables

Look at this program that makes Maths Man count to 10.

It makes use of the say block 10 times to say the numbers 1 through 10.

If we wanted to make Maths Man count to 1000, we would need 1000 of these blocks.

This is not efficient code, but what is the solution? How can we make it more efficient?

It is tempting to make use of the REPEAT (n) block...

Using the REPEAT (n) block and the SAY block would make Maths Man repeatedly say the number 1.

What we want is some way of changing the number inside the repeat loop, so that each time the loop repeats, the number changes (increases) by 1.

A variable is a value that is stored in Scratch's memory and can change during the program.

To use a variable we must first create the variable and give it a name.

If you go to the Orange Variable Block list you will see that there are no blocks, only two buttons.

Click on the 'Make a variable' button to bring up a dialogue box where you can give your new variable a name.

Names of variables are important because they help you identify what you are storing in it.

If you are only using one variable, this is not too much of a problem, but as your programs get more advanced you may end up using lots of variables and if they are not named properly it can get very confusing and easy to use the wrong one!

Watch the video below to see how to use a variable and a repeat loop to count to 10.

Scratch 4 - Variables.mp4

Here is the example code that is used to make the Maths Man count to 10.

Challenges

Create programs that will:

    • make Maths Man say his 2 times tables.

    • make Maths Man say his 5 times tables.

You can download a blank MathsMan project at the bottom of the page.

Press the Green Flag to see the Maths Man say the Two Times Table

Key Words

Variable

A place where data is stored whilst a program is running. The data value can change during the program

Identifier

A name given to a variable so you know what the variable is used to store.


Files

Below are the files you will need to complete the challenges.