# Using variables in a Scratch program

In Scratch you can do more than just set and change variables; you can use them in calculations and comparisons, or store user input - read more here.

In this step, I’m going to show you more things that you can do with the variables you’ve created in Scratch. As well as using the change block, you can use other mathematical operators to manipulate numeric variables. You can also use selection to make decisions based on the value of a variable.

### Changing variable values

You may want to make a variable change by a regular amount, for which you can use a count-controlled loop. The code below:

• Sets my number to 0 before the loop
• Runs the loop 10 times:
• Waiting for a second each time so that you can see the current value of my number
• Increasing the value of my number by two each time

When the loop finishes running, the final value of my variable is 20.

### Operators in Scratch

Scratch includes an Operators category with blocks that allow you to perform calculations or compare values in your programs. The four blocks for performing simple calculations are add +, subtract -, multiply * and divide /.

You can use these blocks to set a variable’s value:

In the example above, Scratch will set my number to 10 when it runs the first block. When it runs the second block, it will set the value of my total to my number multiplied by 8, which is 80. my number will keep the value 10.

You can even set a variable to a new value, based on the current value of that variable:

In this example, Scratch will set my number to 50 when it runs the first block. When it runs the second block, Scratch will take the current value of my number, which is 50 from the first set … block, and divide it by 2 making the new value 25. It will then set my number to this new value, so my number will finish this sequence with the value 25.

### Random numbers

The pick random operator allows you to generate random numbers in your programs. When you use the block you need to set the range of numbers that Scratch will choose from. In the example shown, Scratch will pick a number between (and including) 1 and 10.

Note: If you set the range using whole numbers, Scratch will pick a random whole number. If you set the range to include decimal places e.g. 0.5 to 10 Scratch will pick a random number between those values with six decimal places.

You can set the value of a variable to a random number by combining set my variable to with the pick random block.

### User input

Scratch includes a number of built-in variables that you can use as a programmer. Earlier this week you created a quiz and used the ask … and wait block. When a user types in a response to ask …, Scratch stores this response in a built-in variable named answer.

You can use Scratch’s built-in variables in combination with your own. Your program might take a user’s answer and perform a calculation using it. For example, you could ask a user to enter a distance in kilometres and use their answer to calculate the equivalent distance in miles.

### Making comparisons

Scratch includes operators which you can use in your programs to make comparisons between two values. Your program can check if a value is equal to, greater than, or less than another value.

#### Using selection with the results of comparisons

I’ve created a simple program to demonstrate how you can use an if … then … else block and the Operators blocks together. My program picks a random number between 1 and 10 and stores that in a variable I’ve called my number. The program asks the user to type in a number between 1 and 10. I’ve then used selection containing a condition — “user’s answer more than the random number”. If the condition is true (the user’s number is higher), the user is told this using the first say … block. If the condition is not true, the user’s number must be the same or less than the random number, and the second say … block is run. Lastly, my program then tells the user what my number is.

(my number) then, say "Your number is more than the number I picked" for (5) seconds, else, say "Your number was the same as or less than the number I picked" for (5) seconds, end, say "My number was" for (2) seconds, say (my number) for (2) seconds”>

## Next step

In the next step, I’ll show you how you can use join to get a sprite to say things which combine variables with other text. You’ll then use repetition, selection, and variables to create a times tables test.