Studyfin Studyfin ✨ Sign up free

Variables

Grade 8 · Coding · Free lesson

Hey there! Ready to learn how coders store and change information? Let's explore how variables work!

In coding, a variable is like a labeled box that holds information. You can store numbers, words, or true/false values inside this box to use later in your program.

ai generated

We use variables to modify or change data as our program runs. For example, when you score a point in a game, your score variable increases by one.

SCORE 10

To change a variable, we assign it a new value. If the variable 'score' is 10, and we add 5, the old value is replaced, and 'score' becomes 15.

Updating 'score' Variable 10 15
✏️ Worked example

Let's analyze how a variable changes in a game. Imagine you start a game with a variable named 'health' set to 100. During level one, a monster hits you, and you lose 20 health points. How do we program this change step-by-step to modify the health data?

  1. First, we create and initialize the variable. We set 'health = 100'.
  2. Next, we identify the event that changes the data. In this case, the monster hits your character.
  3. Then, we write an equation to modify the variable. We subtract 20 from the current health.
  4. Finally, we assign the new value back to the variable: 'health = health - 20'. The variable 'health' now holds the value 80.
math
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons