Variables
The variable concept in programming is a way to store information in a program. It’s like having a box with a label on it that you can put different things in. For example, you could have a box called “age” and you can put any number like 8 in it. Then you can use that number in your program.
Data Types in Variables
Data type identifies the type of data that the declared variable can hold. Thus, it indirectly helps the computer to understand what operations need to be performed on those variables.
Let us now understand what are the common data types that we can use in programming:
Integer Data Type: Integer data type variables store integer values only. They store whole numbers which have zero, positive and negative values but not decimal values.
Example of declaring an Integer variable:int a = 2;
Floating Point Number Data Type: Floating-point numbers are used to store decimal values. They hold real numbers with decimal values.
Example of declaring a floating-point number variable:float a = 1.1;
Character Data Type: Character type variables are used to store character values. Syntax of declaring a character variable is specific to the programming language that you are using. If a user tries to create a character variable and assign it with a non-character value, the program will throw an error. The character data type is the smallest data type in programming.
Example of declaring a character variable:char a = 'w';
String Data Type: To extend the character data type, a user may have a requirement to store and perform an operation on a sequence of characters. In such cases, the String data type is present to fit the gap. The String data type stores value in a sequence of characters i.e., in String format.
Example of declaring a String variable:String a = “I am a String Variable”;
Boolean Data Type: There is a subtype of Integer Data Type called “Boolean Data Type”, which stores values in Boolean type only i.e. “true” or “false”. Users can choose between the data types for variables as per program needs and assign variables an appropriate data type.
Example of declaring a Boolean variable:bool a = true;
Naming Rules
As we have understood till now, variables in a programming language are basically like nouns. Every variable in a program is unique. To identify these variables uniquely, the user needs to allocate them a unique name. This name acts as an identifier for that variable. In programming, a user is not allowed to use the same name of a variable more than once.
Below are some rules for naming a variable:
- A variable name cannot start with a number, it must start with an alphabet or the underscore (_) sign.
- A variable name is case-sensitive. Sum and sum are different variables.
- A variable can only contain alphanumeric characters and underscore.
Variables in Pictoblox
Let’s see how you can create a variable in PictoBlox. Follow the steps below:
- Open the
Variables - Select
Make a Variable.
- A small dialogue box will appear in the center of the screen asking for the variable name; give it any name of your choice.
- Click
OK. That’s it!
Manipulating a Variable Using Blocks
Now, let’s see how to assign values to a variable, change them, etc.
- The set () to () block will set the specified variable to the given value be it a string or number.
- The change () by () block will change the specified variable by a given amount.
- Reporter block simply holds its variable and reports it when the variable is called.
Activity 1: Tracking a Sprite’s Positions Using Variables
In this activity, we will track the change in Tobi’s position using the up, left, down, and right arrow keys on the keyboard. We will track his X and Y positions using variables.
Coding Steps
Follow the steps below:
- Open
PictoBlox App, go toMy Space, and click on+button to create a new file. - Create two variables X and Y. We’ll use them for Tobi X and Y positions respectively.
- Add a new backdrop named
Xy-grid. You can refer the process of adding the backdrop from the Lesson 1 Activity 2 Step 2. - Add the when flag clicked block to start the script.
- Next, set the X and Y positions as 0 by setting the X and Y variables as 0 using the set () to () block from the
Variablespalette. - Now, add a forever block below the set () to () block.
- Then, from the
Motionpalette, add a go to x:() y:() block and drag and drop the X and Y variable blocks inside it as shown. This will set Tobi’s position to the current value of X and Y. Now, to make Tobi move around and track his position, we need to change the values of X and Y. How do we do that? Here’s how:
- To move left, we will change X by -10 whenever the left key is pressed. Add a when () key pressed block from the
Eventspalette and change X by -10 by using a change () by ().
- To move right, we will change X by 10 whenever the right key is pressed. Repeat the above step and select the right arrow key from the drop-down of the hat block and change X by 10 in the change () by ()
- Follow the above steps to make Tobi move up when the up arrow key is pressed. We will change Y by 10 whenever the up key is pressed.
- For the above steps to make Tobi move down when the down arrow key is pressed. We will change Y by -10 whenever the up key is pressed.
- Click on the green flag to start the project. Click on the stage button next to the green flag to enlarge the stage. Click the arrow buttons to move Tobi around.
- Save the file as
Tracking Sprite.
Activity 1: Output
Operators
Operators are symbols that tell the computer to perform specific mathematical or logical manipulations.
There are five main arithmetic operators in PictoBlox:
S No | Operator | Definition | Image |
1 | Addition | Adds values on either side of the operator. | |
2 | Subtraction | Subtracts the right-hand operand from the left-hand operand. | |
3 | Multiplication | Multiplies values on either side of the operator. | |
4 | Division | Divides the left-hand operand by the right-hand operand. | |
5 | Modulus (Remainder) | The modulus operator (%) calculates the remainder when two variables are divided. Please note that this operation can only be performed on an integer. |
Activity 2: Addition Bot
In this activity, you’ve to make a script that:
- Asks the user to input Number 1.
- Asks the user to input Number 2.
- Performs addition operation on Number 1 and Number 2 and saves the result in a variable answer.
- Display the answer to the user using a say () for () seconds.
Coding Steps
Follow the steps below:
- Create a New file in
PictoBlox App. - Add two ask () and wait blocks from the
Sensingpalette to get the input from users for Number 1 and Number 2. Using the ask () and wait block will add an input box (with the specified text above it) at the bottom of the Stage. The user can write the answer in it and submit it which is then stored then in the answer.
- Next, make 3 variables:
- Number 1: To store the value of the first input.
- Number 2: To store the value of the second input.
- Result: To store the final output value.
- Place two set () to () blocks, one below each ask () and wait block to store the input values in Number 1 and Number 2.
- Place another set () to () block at the end and choose Result from its drop-down. Place a () + () block (addition operator block) in the white space and add Number 1 and Number 2.
- Make Tobi say the result out loud using the say () for () seconds You will use multiple join () () blocks from the
Operatorspalette to create this. - Complete the script by adding a when flag clicked block.
- Save the file with the name
Addition Bot.
Activity 2: Output
Make sure you finish and submit the assignment, as well as take the quiz, before moving on to the next lesson.