Studyfin Studyfin ✨ Sign up free

Variables

Grade 6 · Coding · Free lesson

Hey there! Today we are going to explore variables, which are like magical, labeled boxes in coding that hold information for us.

In coding, a variable is a named container that stores information. We can use variables to keep track of things like game scores, player names, or the speed of a character.

just

We can change, or modify, the data inside our variable whenever we want. When we put a new value into the variable, the old value is replaced and forgotten.

Variable: score 10 15

Let's analyze how changing a variable affects a program. Imagine a game where your score starts at 0, and you earn 5 points for every gold coin you grab. By changing the score variable, the game screen updates to show your new total instantly!

Score: 0 P
✏️ Worked example

We want to write a program that tracks a player's health. The player starts with 3 hearts. When they touch a spike, they lose 1 heart. Let's explain how to use a variable to modify this data step-by-step.

  1. First, we create a variable named 'hearts' and set its starting value to 3.
  2. Next, we write a rule in our code: 'If player touches spike, subtract 1 from hearts.'
  3. Then, we run the game. The player touches a spike!
  4. Finally, the program modifies the variable. It calculates 3 minus 1, and stores the new value of 2 inside the 'hearts' variable.
math
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons