Scratch: Variables and Operator blocks

About This Tutorial
Variables are used whenever a value must be stored. In this tutorial, we will learn about variables, types of variable and how to use them in Scratch.
Tutorial Info
Programming Platform:
Difficulty:
Estimated Time (hrs):
2

Introduction

Variables

A variable is a like a storage container can take or store information. It is used to store this information so that we can refer or/and manipulate it at a later stage in a computer program. It also provides a way of labeling data with a descriptive name, so that the program can be understood more clearly by the reader. Its sole purpose is to label and store data in memory. This data can then be used throughout the program. Hence, they are critical while writing re-usable code.

Variables can only hold one value at a time. 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.

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). Below are some examples:

  • Hello, world
  • 123
  • 14
  • 0
  • -321
  • true
  • {nothing — empty string}

Types of variables

There are two types of variables, public (global), and private (local):

Global Variables

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.

Local Variables

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.

Use of a Variable

Variables are used whenever we need to store a value, i.e. if a project requires the user to input a name and then remember that name. This 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:

  • Normal readout: The typical Stage monitor that you get when you first show it. This shows both the name and the value of the variable.
  • Large readout: This shows only the value, and in a larger font than the normal readout.
  • Slider: This lets the user manually change the value of a variable inside a range which can be defined by right-clicking the Stage monitor on the project screen and choosing two numbers.

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.

Variable Display

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”.

Checkbox variable

There are five different blocks relating to variables:

 

NameBlockFunction
Set VariableThe block will set the specified variable to the given value: a string or number.
Change VariableThe block will change the specified variable by a given amount.
Show VariableThe block shows the specified variable's Stage monitor.
Hide VariableThe block hides the specified variable's Stage monitor.
VariableThe block simply holds its variable

How to create a variable in Scratch

  1. Open mBlock; if already open and working on a project, save that project. Then, click on New.
  2. Go to the Block palette and click on the Data&Blocks palette.
  3. You’ll see three options in the Block Palette – Make a Variable, Make a List, Make a Block; click on Make a Variable.
  4. A small dialogue box will appear in the centre of the screen asking for the variable name. give it any name of your choice.
  5. Click on OK.

Operators

Operators are characters that perform functions on data; it be either one variable or constant, or more that one. In mBlock, there are different types of operators in the Operators palette such as arithmetic operators (+, -, *, /), logical operators (and, or, not), and relational operators (<, >, =).

Operator Blocks in Scratch

Operator blocks are color-coded light-green, and are used to script math equations and string handling.

There are currently 17 Operators blocks: 6 Boolean blocks and 11 Reporter blocks.

Boolean Palette

Boolean data type is a data type with only two possible values: True or False.

Given below are the several Boolean blocks: 

NameBlock

Function
Less Than

The block checks if the first value is less than the second value. If it is less, the block returns true; if not, it returns false. This block works with letters too, as well as numbers. In Scratch, letters at the top of the alphabet (e.g. a, b, c) are worth less than letters at the end (e.g. x, y, z).

Equals to

The block checks if the first value is equal to the other value. If the values are equal, the block returns true; if not, false. This block is not case-sensitive.

Greater than

The block checks if the first value is greater than the other value. If the second value is less, the block returns true; if not, it returns false.

AND

The block joins two Boolean blocks so they both have to be true to return true. If they are both true, the block returns true; if they are not all true or none true, it returns false.

OR

The block joins two Boolean blocks so any one of them can be true to return true — if at least one of them is true, the block returns true; if neither of them are true, it returns false.

NOT

The block checks if the Boolean inside it is false — if it is false, the block returns true; if the condition is true, it returns false.

Reporter Blocks

Reporter blocks returns values.

Given below are some of the reporter blocks:

NameBlock

Function
AdditionThe block adds two values and reports the result.

The numbers can be typed directly into the block, or Reporter blocks can be used instead.

This block can be stacked inside itself; this can be used to fit more numbers in.
SubtractionThe block subtracts the second value from the first and reports the result.

Multiplication

The block multiplies the two values and reports the result.

Division

The block divides the second value from the first and returns the result.

If the first value is not evenly divisible by the second, the reported value will have decimals. To find the remainder instead, use the () Mod () block.

Note, dividing by 0 in the online editor will return infinity, 0, or negative infinity. It depends on if the numerator is positive, 0, or negative; respectively.

RandomThe block picks a pseudorandom number ranging from the first given number to the second, including both endpoints. If both numbers have no decimals, it will report a whole number. For example, if a 1 and a 3 were inputted, the block could return a 1, 2 or 3. If one of the numbers has a decimal point, even .0, it reports a number with a decimal. For example, if 0.1 and 0.14 were given, the output will be 0.1, 0.11, 0.12, 0.13, or 0.14.

JoinThe block concatenates, or "links" the two values together and reports the result — for example, if "hello" and "world" were put in the block, it would report "helloworld".

Letter

The block reports the specified character of the given text. Even though the block says "letter", it will report all characters, including letters, numbers, symbols, and even spaces.

LengthThe block reports how many characters the given string contains.

ModThe block reports the remainder of the division when the first value is divided by the second. For example, when 10 is put in the first input and 3 in the second, the block will report 1; 10 divided by 3 gives a remainder of 1.

RoundThe block rounds the given number to the nearest integer. It follows the standard rules of rounding; decimals that are .5 or higher are rounded up, whereas decimals less than .5 are rounded down.

Advance math

The block performs a specified function on a given number and reports the result.

The function can be changed by clicking the down arrow and selecting a new function from the drop-down menu.

The above information is referenced from Scratch Wiki.

Project

Let us use these concepts to make a project, where the Panda, the default sprite in mBlock, 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.

Follow the steps below to write the script:

If you’re working with evive in Scratch mode for the first time, or have uploaded the Arduino firmware previously, first upload the Scratch fimware; you can find the procedure to do the same here
  1. Open mBlock; if already open and working on a project, save that project. Then, click on New.
  2. Go to the Events palette and drag and drop the when ‘flag’ clicked block.
  3. Go to the Data&Blocks palette and create a variable Celsius. Then, drag and drop the set () to () block below the when ‘flag’ clicked block, and select Celsius  from the drop-down menu. Initialise  the variable, i.e. set the variable to zero. It is recommended to initialise the variable with some value at the start of program, because once you complete executing the program the value at the end will be the new value of the variable, and it doesn’t get reset when you execute it again which may lead to wrong results.
  4. Create another variable Fahrenheit. Then, drag and drop the set () to () block below the when ‘flag’ clicked block, and select Fahrenheit from the drop-down menu. Set the variable to zero.
  5. You would also want to hide the variable from the stage. To do so, go to the Data&Blocks palette and drag and drop the hide variable block. Select Celsius from the drop-down menu.
  6. Repeat step 6. This time, select Fahrenheit from the drop-down menu.
  7. To make the project catchy you can make the Panda say something to the user. For this, go to the Looks palette and drag and drop the say () for () secs block. In the first space write any phrase that you want the Panda to say, such as ‘Hi, I’m Metric Panda, and I can convert temperature values from Fahrenheit to Celsius.’ and in the second space write 5.
  8. In order to take the temperature value from the user as an input, drag and drop the ask () and wait block from the Sensing palette below the  say () for () secs block.In the space write Please enter the temperature value in Fahrenheit. The benefit of using the ask () and wait block is that it has a variable dedicated to store its response; it is named answer and it is right below the ask () and wait block in the Sensing palette.
  9. To store the value a variable is necessary. Go to the Data&Blocks palette and drag and drop the set () to () block below the ask () and wait block. Select Fahrenheit from the drop-down menu and in the second space drag and drop the answer variable block from the Sensing palette.
  10. Drag and drop another set () to () block and select Celsius from the drop-down menu. To convert the value to Celsius we must use the equation. For this we will use some operator blocks from Operator palette. Using these operator, we will break the above conversion into simple operation sets: 1 subtraction, 1 division and 1 multiplication. Go to the Operators palette and drag and drop the round () block inside the space of the  set () to () block. Now, inside this block, drag and drop the () * () block. In the first space of the () * () block drag and drop the () – () block, and in its second space drag and drop the () / () block.
  11. In the first space of the () – () block drag and drop the Fahrenheit variable block from the Data&Blocks palette and in the second space write 32
  12. In the first space of the () / () block write 5 and in the second space write 9.
  13. Go to the Looks palette and drag and drop the say () for () secs block. In the first space write ‘The temperature value in Celsius is: ‘ and in the second space write 2.
  14. Now, drag and drop the show variable () block from the Data&Blocks palette below the  say () for () secs block and select Celsius from the drop-down menu.
  15. Repeat step 15 but this tie select Fahrenheit from the drop-down menu.
  16. Go to the Looks palette and drag and drop the say () for () secs block. In the first space drag and drop the Celsius variable block and in the second space write 5.

Below is the complete script:

Click on the green flag to run the script.

Click on the red octagon, next to the green flag to stop the script.

This Helped
This didn't help
Close Menu
×

Cart