Studyfin Studyfin ✨ Sign up free

Variables

Grade 7 · Coding · Free lesson

Hey there! Ready to unlock a super cool coding power? Today, we will learn how variables store and change data in a program!

Think of a variable as a labeled storage box in your computer's memory. Inside this box, you can store a single piece of data, like a number or a word.

The magic of a variable is that we can change, or modify, the data inside it while our program is running. When we put a new value in the box, the old value is replaced.

score 10 15

We use variables to modify how a program behaves. For example, changing a speed variable from slow to fast instantly makes a game character move quicker across your screen.

Speed Variable: Slow → Fast!
✏️ Worked example

Let's analyze how a game program uses a variable to track and modify a player's score. The player starts with 0 points, gains 5 points for catching a star, and then loses 2 points for bumping into an obstacle. Let's trace the variable's value step by step.

  1. First, we create a variable named 'score' and set its starting value to 0. Our box now holds the number 0.
  2. Next, the player catches a star! We modify the variable by adding 5 to the current value. The computer calculates 0 + 5, and updates 'score' to 5.
  3. Then, the player bumps into an obstacle. We modify the variable again by subtracting 2. The computer calculates 5 - 2, and updates 'score' to 3.
  4. By analyzing this process, we see that the program successfully used and modified the 'score' variable to keep an accurate, changing record of the game!
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons