Consider a simple program that adds 2 + 2 and outputs the result to the screen. That’s great, but what if you want to add different numbers? Do you want to have to go into the program and change the numbers that it adds? That sounds like some trouble! Also, this is a very simple program, what about complex programs? Changing numbers would not be feasible. Wouldn’t it be easier to create 2 variables that represent the first and second number (say ‘a’ and ‘b’ to store the first and the second number respectively), and get input from the user as to what the values of those variables are? Then all you have to do is instead of writing print 2+2 (or whatever the right syntax is in your programming language), you write print a+b. It couldn’t be simpler.
Variables are like a storage container. They are used to store information to be referenced and manipulated in a computer program. They also provide a way of labelling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. Their sole purpose is to label and store data in memory. This data can then be used throughout your program. Hence, they are critical to writing re-usable code. Variables can only hold one value at a time, unlike lists. These values can be either numbers or strings — any text. In SCRATCH, clicking on an isolated variable in the scripts area displays a small bubble reporting the value of the variable. Variables must be created prior to when the project actually runs.
Contrary to algebraic variables (which are usually unknown), the variables in Scratch and other programming languages are simply known values. In fact variables can contain text (strings), numbers, or Booleans (true/false values). Some examples are below:
You can create a variable by clicking on create variable button in Variable palette. This creates a new variable in the palette. You can create any number of variables you want.
There are two types of variables, public (global), and private (local):
By default, when a variable is created, it is a global variable. Global variables can be read and changed by any sprite, or the Stage.
In general, local variables (or private/personal) are variables which are accessible only to specific parts of the code. They are created in the same way as global, but the option For this sprite only” is selected in the variable creation dialog. Personal variables can only be changed by their “owner”, but can be read by other sprites using the () of () block. The Stage cannot have local variables.
Local variables are extremely useful when you want to create a template sprite which needs to be duplicated and edited. For example, in a game where one must pop bubbles, a “bubble” sprite should be made which has personal variables like “speed” and should be programmed independently. Then it should be duplicated until there are enough bubbles. Since each bubble has an individual “speed” variable, they will not interfere with each other unlike if “speed” was a global variable. Consider each “speed” variable as the personal property of a bubble.
Variables are used whenever a value must be stored — i.e., if a project requires the user to input a name and then remember that name, the name would be stored in a variable. With this, the name can be retrieved at any time; all the project has to do is check the value (which is the name).
A Stage monitor is available for every variable to display the value of a variable on the stage. There are three types of Stage monitors for a variable:
The form of the stage monitor can be changed by double-clicking or right-clicking it and selecting the option that is wanted, or clicking it using the grow/shrink sprite tool.
It can be chosen whether to hide or show a variable on the stage by clicking the tick box in the variable panel or by right-clicking it and selecting “hide”.
There are five different blocks relating to variables:
Let us use these concepts to make a project, where the Scratch cat will ask you to enter a temperature in Fahrenheit and displays the temperature in Celsius.
Given below is the conversion formula:
C = (F-32)*(5/9)
Where, C is temperature in Celsius
F is the temperature in Fahrenheit.
To start with the project, you need to make two variables Celsius and Fahrenheit and store the values. When the user enters a value you will store it in the Fahrenheit variable and when you convert the temperature into Celsius, you store it in Celsius.
Follow these steps to make the project:
Yeah, you have completed you project and now you can start executing your script by clicking green flag.
In this tutorial we have learned what are variables, how to create them and how to use them in Scratch. Next, we will see how to use conditional statements.