Studyfin Studyfin ✨ Sign up free

Variables

Grade 5 · Coding · Free lesson

Hey there! I am Studyfin, your coding buddy. Today, we will explore variables, which are like magical, labeled boxes in computer science!

Imagine you have a box where you keep your game score. In coding, we call this box a variable. A variable holds a piece of data that can change as you play.

To use a variable, we first give it a clear name like 'score'. Then, we put a starting value inside it, such as the number zero.

score 0

The coolest thing about variables is that we can modify, or change, their data. If you grab a gold coin, the program adds ten points to your score box instantly!

score 10 +
✏️ Worked example

Let us think like a programmer! Imagine you are building a video game. Your player starts with 3 lives. During the game, they hit a spike and lose 1 life. How do we use a variable to keep track of this change step by step?

  1. First, we create our variable. We will give it a name that makes sense, like 'lives'.
  2. Next, we set the starting value of our variable. Since the player starts with 3 lives, we set 'lives' to 3.
  3. Now, we write a rule for when the player hits a spike. This rule will modify our variable.
  4. The rule says: 'Set lives to current lives minus 1'.
  5. We run the rule! The computer looks inside the 'lives' box, sees 3, and subtracts 1.
  6. Finally, the computer stores the new number, 2, back inside the 'lives' box. The variable has successfully changed!
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons