Skip to content

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

  1. Click on the Variables section in the Block Palette (see Image 1).
  2. Click the blue Create Variable… button (see Image 2).
  3. A popup will appear (see Image 3). Type the name of your variable (e.g., name, favoriteColor, or animal) and click OK.
  4. The new variable will appear in the dropdown menu on the variable block (see Image 4).
  5. Drag the block set [variable] to [value] to the Workspace.
  6. Assign an initial value (e.g., "John" for name or "Blue" for favoriteColor) in the block.

Example: Setting a Variable

To create a variable called name and set its value to "Sophie":

set [name] to "Sophie"

This tells the program, “Remember that name is Sophie right now.”


Activity 1: Create Your Own Variable

  1. Create a variable and name it something fun, like favoriteAnimal or bestFriend.
  2. Set its value to anything you like (e.g., "Dog" or "Emma").
  3. 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:

  1. Drag the Set [variable] block into the Workspace.
  2. Select your variable from the dropdown menu (e.g., score or favoriteNumber).
  3. Choose the operation:
    • Use = to assign a completely new value.
    • Use += to increase the variable’s value.
    • Use -= to decrease the variable’s value.
  4. Enter the value you want to add or subtract (e.g., 5).
  5. Use the print block to display the variable after the change.
  6. Run the program to see the updated value.

Example: Adding to a Variable

Let’s create a program that keeps track of points:

  1. Create a variable called score and set it to 0.
  2. Drag another block to add 10 to score using the += option.
  3. Use a print block to display the new value of score.

The code will look like this in EduBlocks:

Set [score] = 0
Set [score] += 10
Print [score]

When you run the program, the output will show 10.


Activity 2: Experiment with Updating Variables

  1. Create a variable called steps and set it to 0.
  2. Add 2 to steps using the += option.
  3. Subtract 1 from steps using the -= option.
  4. 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

  1. Create a variable called name and set it to "Alex".
  2. Use a print block to display: "Hello, " + name + "!".
  3. Run the program to see the personalized greeting.

Program 2: Favorite Things

  1. Create variables for favoriteColor and favoriteAnimal.
  2. Assign values to each variable (e.g., "Blue" for favoriteColor and "Elephant" for favoriteAnimal).
  3. 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:

  1. Why are variables useful in programming?
  2. Can you think of a game or app that uses variables? (Hint: think of names or favorite settings.)
  3. 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!

Vocabulary Review