In this lesson, students will learn what variables are and how they are used in coding. They will learn how to create variables, store values in them, and use those variables to control actions in a program. |
Students will be able to:
|
Introduction to Variables In this lesson, you will learn how to create and use variables in RoboBlocky. This video explains why variables are useful, how to declare and name them, and how to assign values. It also shows how to use variables in code, update their values, and print them to the console.
1. Why Do We Store Values? When we write programs, we often need to store values to use them later or in more than one place.
2. What Is a Variable? A variable is a place in a program where you can store a value, such as a number. You can think of it like a labeled container or a storage box. The label is the name of the variable, and what is inside the box is the value. For example, you might create a variable called sideLength and store the number 4 in it. Then, you can use sideLength in different parts of your program instead of writing the number 4 over and over again.
3. How to Create or “Declare” a Variable We create a variable using the set variable block, found in the Variables block menu.
Click the drop-down menu to see many options. Select "New variable" to create a new variable and give it a name. Creating a variable and giving it a name is also called declaring a variable.
4. Give the Variable a Descriptive Name It is good practice to use names that tell you what the variable is for. For example, distance, height, or sideLength.
5. Assign a Value to the Variable To store a value in a variable, attach a blue number block to the set variable block. This is also known as assigning a value to the variable. In this example, the variable named distance has an assigned value of 4. ![]()
6. Using a Variable After we create a variable, it will show up in the Variables block menu.
Note: If the newly created variable does not appear right away, click the Variables menu button twice to refresh it. We can now drag the variable block into other code blocks that use a blue number block, like the driveDistance block. ![]()
![]()
7. Variables Can Change During a Program The word "variable" comes from the fact that its value can vary, or change. You can update a variable's value later in the program using another set variable block. ![]()
8. Printing a Variable We can use the print variable block to print the name of a variable and its value.
![]()
When you create a variable, the computer sets aside space in its memory to store the value. Memory is the part of a computer where data is stored so the program can use it later. Data is the information the computer uses or stores, such as numbers, text, or images.
|
|
The example creates a variable named distance and sets it to a value of 6. Then it uses the variable to drive the robot forward 6 inches. The code then turns the robot and assigns the distance variable a new value of 4, before using it to drive the robot 4 inches forward. |
The example creates a variable named sidelength and sets it to a value of 4. Then, it uses the variable in four driveDistance blocks to have the robot trace a square. |
The example creates a variable called number and sets it to 5, then uses a print variable block to print its name and value in the console. Next, a second variable called sum with the value 5 + 3 is created. Its name and value is printed in the console using a second print variable block. |
Grid Size: S M L | Simple View: | MinView: |
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
|
|||||||||||
|
|||||||||||