In Scratch, variables are like containers that you can use to store values, such as numbers or strings of text. You can use variables to keep track of important data in your program, and to make your program more flexible and interactive.
There are two types of variables in Scratch: global variables and local variables.
Global variables are variables that can be accessed and modified by any script in your program. They are useful for storing data that you want to use in multiple scripts or that you want to share between different sprites.
Local variables are variables that are only accessible and modifiable by the script in which they are defined. They are useful for storing data that is only needed in a particular script and that you don’t need to share with other scripts or sprites.
Here are some examples of how you can use variables in Scratch:
Variables are a powerful tool in Scratch that can help you create more interactive and flexible programs.
1) A variable is declared by clicking on the variables category.
2) Then we click on create a variable.
3) Then we give a name to this variable.
To learn how to use variables in Scratch, we’ll create a program that calculates the sum of two numbers.
First, we want to create 3 variables a , b and somme to store the three numbers.
Then we assign to the two variables a and b two values using these two instructions:
Next, we need to sum the variables a and b and store the result in the somme variable:
Here is the final program:
Finally here is the result of the program on the Scratch stage: