Variables are used to store some data while a program is running.
A variable can store all sorts of data including, numbers, letters or text.
Variables are used in many computer programs.
Each variable has a unique name to identify it and indicates the data stored in it.
For example, a variable used to store your points in a game might be call score because it is used to keep your score. If two people are playing the game the variables might be called score_player1 and score_player2.
You met a variable in Lesson 3.
The ask block in Scratch makes use of a variable to store the answer that the user types in.
The data that the user inputs is automatically stored in the variable named answer for use later in the program.
See another example below.
The program asks the user What's your name?The user types their name and presses enter and the name is stored in the answer variable.
The program outputs the text, Your name is... .
The program then outputs the name stored in the answer variable for 2 seconds.
One way to think of a variable is like a whiteboard that text or numbers can be written onto and wiped off.
This data can be changed by erasing the old data and replacing it with some new data.
When creating a variable in a program it is very important to give your new variable a name which reminds you about the data it is going to store.
In this example a variable is needed to store a player's score in a computer game, so an obvious name for this variable is 'score'.
At the start of the game, score is 0 because the player has yet to score any points.
We say that we have assigned 0 to the score variable.
When a player scores some points the data in the variable will change.
If the player wins two points, score will be changed to 2.
Later in the game, the player wins 10 more points so the value of score will be changed to 12.
So you can see how the data inside a variable can change, it was 0, then it became 2, and now it is 12 but it is the same, score variable.
Watch the video below for another explanation.
Q11. A variable called points is used to store the number of points a player has in a computer game.
If a player began a game with 0 points, won 10 points, and then lost 4 points, what would the final value of points be?
Click on this link sjcs_pony
Click See inside and look at the code in the girl sprite for an example of a variable.
Click on the green flag too play the game and find out how the variable is used to keep your score as you play the game.
Click See inside and look at the code in the girl sprite for an example of a variable.
Q12. What is the name of the variable that is used to store a players score in sjcs_pony?
Q13. How many points are added when the girl sits on the saddle?
Q14. How many points are needed to win the game?
Study the scripts below to make sure that you understand how a variable is created and used in a game.
Click Make a Variable to create a new variable in your
program.
Give your variable a name that reminds you about the data stored by it - in this case, score.
Add a tick to display the variable on the screen.
The set to block is used to assign some data to the variable, in this case 0.
The change by block is used to add or subtract value from the variable.
The show variable block will display the variable on the stage.
The hide variable block will NOT display the variable on the stage.
Study the script below to see how this variable will is used to store the points data as the game is played.
On a gren flag event, the script on the right sets the value of score to 0 at the start of the game.
If a score of 100 is reached the program will tell the user You win!
As the game is played, if the player sprite touches the basketball sprite then the change by block is adds 1 to the score variable.
hide is used to make the basketball sprite disappear.