Chapter 2
Updated Chapter 2: Working with Variables and Data
Welcome back, young programmer! In this chapter, we’ll explore variables and how they help us store information in our programs. Using the EduBlocks interface, you’ll create and use variables step-by-step, following the visuals to guide you. Let’s get started!
2.1 What Are Variables?
Imagine you have a box labeled “Favorite Snack.” Today, you might put cookies inside it, and tomorrow, you could replace them with chips. Variables work the same way—they store values that can change over time.
Why Do We Use Variables?
- To store and update information, such as a player’s name or favorite color.
- To personalize user experiences, like greeting someone by their name.
- To make programs dynamic and interactive.
2.2 Creating Variables in EduBlocks
Let’s learn how to create variables in EduBlocks! Follow these steps while referring to the screenshots for guidance:
Steps to Create a Variable
- Click on the Variables section in the Block Palette (see Image 1).
- Click the blue Create Variable… button (see Image 2).
- A popup will appear (see Image 3). Type the name of your variable (e.g.,
name
,favoriteColor
, oranimal
) and click OK. - The new variable will appear in the dropdown menu on the variable block (see Image 4).
- Drag the block
set [variable] to [value]
to the Workspace. - Assign an initial value (e.g.,
"John"
forname
or"Blue"
forfavoriteColor
) in the block.
Example: Setting a Variable
To create a variable called name
and set its value to "Sophie"
:
This tells the program, “Remember that name
is Sophie right now.”
Activity 1: Create Your Own Variable
- Create a variable and name it something fun, like
favoriteAnimal
orbestFriend
. - Set its value to anything you like (e.g.,
"Dog"
or"Emma"
). - Share your variable with a classmate and explain what it represents.
Updated Chapter 2: Working with Variables and Data
2.3 Using Variables in Programs
Once you’ve created a variable, you can use it to:
- Display its value.
- Update its value dynamically as the program runs.
Updating a Variable
In EduBlocks, you can update a variable’s value using the “Set” block with multiple options (as shown in the screenshot). The options include:
=
: Assigns a new value to the variable.+=
: Adds a specific value to the current value of the variable.-=
: Subtracts a specific value from the current value of the variable.
Steps:
- Drag the
Set [variable]
block into the Workspace. - Select your variable from the dropdown menu (e.g.,
score
orfavoriteNumber
). - Choose the operation:
- Use
=
to assign a completely new value. - Use
+=
to increase the variable’s value. - Use
-=
to decrease the variable’s value.
- Use
- Enter the value you want to add or subtract (e.g.,
5
). - Use the
print
block to display the variable after the change. - Run the program to see the updated value.
Example: Adding to a Variable
Let’s create a program that keeps track of points:
- Create a variable called
score
and set it to0
. - Drag another block to add 10 to
score
using the+=
option. - Use a
print
block to display the new value ofscore
.
The code will look like this in EduBlocks:
When you run the program, the output will show 10
.
Activity 2: Experiment with Updating Variables
- Create a variable called
steps
and set it to0
. - Add 2 to
steps
using the+=
option. - Subtract 1 from
steps
using the-=
option. - Print the value of
steps
after each change to observe how it updates.
2.4 Practical Applications of Variables
Let’s put variables into action by creating simple programs.
Program 1: Personalized Greeting
- Create a variable called
name
and set it to"Alex"
. - Use a
print
block to display:"Hello, " + name + "!"
. - Run the program to see the personalized greeting.
Program 2: Favorite Things
- Create variables for
favoriteColor
andfavoriteAnimal
. - Assign values to each variable (e.g.,
"Blue"
forfavoriteColor
and"Elephant"
forfavoriteAnimal
). - Use
print
blocks to display sentences like:"My favorite color is " + favoriteColor + "."
and"My favorite animal is " + favoriteAnimal + "."
.
Activity 3: Create Your Own Favorite Things Program
Create a program that stores your favorite food, color, and hobby in variables. Print a message for each one, like: "My favorite food is Pizza."
2.5 Discussion: Why Are Variables Important?
Talk with your classmates or write down your thoughts:
- Why are variables useful in programming?
- Can you think of a game or app that uses variables? (Hint: think of names or favorite settings.)
- What creative idea do you have for using variables in your own program?
Congratulations!
You’ve learned how to create and use variables in EduBlocks. In the next chapter, we’ll explore how to use conditionals to make decisions in your programs. Keep up the great work!