Skip to main content

L4: Variables & Math

In progress

Excuse our mess while we finish this page! You can contribute here.

Overview​

In this lesson, students will learn how computers store information using variables.

Students will use variables to:

  • count button presses
  • store numbers
  • update values
  • perform simple math

Using the on button press event blocks from the previous lesson, students will build interactive programs that change over time as buttons are pressed.

Students will also explore how computers use math operations such as:

  • addition
  • subtraction
  • multiplication
  • division

Learning Goals​

Students will:

  • understand what variables are
  • store and update numbers
  • count button presses
  • use math with variables
  • display changing values
  • build interactive programs

Time Required​

30–45 minutes


Materials​

  • CincoBit or PixoBit
  • USB cable
  • Computer or Chromebook
  • MicroBlocks

Teacher Notes​

Variables are one of the most important concepts in programming.

Students should understand:

Variables allow programs to remember information.

Examples of variables in real programs:

  • scores in games
  • player health
  • timers
  • temperatures
  • counters
  • money balances

This lesson keeps the focus on:

  • event-driven programming
  • changing program state
  • simple math operations

What is a Variable?​

A variable is a named-place where a program stores information.

Variables can change while the program runs.

Think of a variable like:

  • a labeled box
  • a scoreboard
  • a container for information

Example:

score = 5

The variable named score currently stores the number 5.


Step 1 - Create a Counter​

From the variable palette, click Add a variable and give your variable name count.

variable

Newly create variables always start with their value set to zero.

You now use set to to set a variable to a number, or use change to increment the number.

Now use when button pressed to increase the count.

Each button press should:

  1. add 1 to the variable
  2. display the new value

variable

The display character block only shows one character. When showing number 0 to 9 it works fine, but when it tries to print 10, you will see 1 only. as 0 is out of the screen area. Can you solve this problem? Hint: Scroll the text!


Step 2 - Add Two Numbers​

Programs can perform math using variables. The change block is adding a number to whatever value the variable is holding, and then storing the results in the same variable.

Example: count = count + 1

This means:

  1. take the current value
  2. add 1
  3. store the new result

If:

count = 3

then after adding 1:

count = 4

Computers can perform many types of math.

OperationExample
Addition3 + 2
Subtraction10 - 4
Multiplication5 * 3
Division12 / 2

We will increment different variables from pushing A and B buttons. We then show the sum of both variables when both buttons A+B are pressed.

Create two variables like we did before: count1 and count2. Increment the right variable when buttons are pressed. The scroll text the sum of both numbers when both buttons are pressed.

The sum math operator is found under Operators palette.

math add

Students can experiment with:

  • different numbers
  • subtraction
  • multiplication

Understanding Program State​

Variables help programs remember what happened earlier.

Without variables:

  • programs forget information immediately

With variables:

  • programs can track changes over time

Examples:

  • score keeping
  • timers
  • player lives
  • temperatures
  • sensor values

Try It​

Can you:

  • count backward
  • count by 5s
  • create a score tracker
  • add two button counters together

Challenge​

Create a simple score system:

ButtonAction
AAdd 1 point
BAdd 5 points

Display the total score after every press.


Real-World Connection​

Variables are used everywhere in computing.

Examples:

  • video game scores
  • bank balances
  • weather apps
  • shopping carts
  • GPS navigation
  • fitness trackers

Programs constantly store and update information using variables.


Troubleshooting​

  • Counter does not change
    • verify the variable is updated inside the event block
  • Wrong numbers appear
    • check math operations carefully
  • Display does not update
    • ensure the display block runs after changing the variable

Vocabulary​

WordMeaning
VariableA place to store information
ValueInformation stored in a variable
AdditionCombining numbers
CounterA variable that tracks changes
StateInformation remembered by a program

Wrap-Up Question​

Why are variables important for games and interactive programs?