When you use apps or play games, they often keep track of changing information, like your score or the number of lives you have left. This is possible because of a computer’s memory, which is a powerful tool in programming. One of the most common ways to use this memory is by creating something called a variable.
A variable is like a container that holds a value. Once you create a variable, it is stored in the computer’s memory and can be used throughout your program. For example, in a game, you might use variables to keep track of the score and the number of lives remaining.
Creating a variable is simple. You start with the word “var” followed by a label, which is a name you give to the variable. It’s helpful to choose a name that describes what the variable is storing. For instance, you might name one variable “score” to keep track of the score and another “lives” for the number of lives.
Once you have your labels, you can assign values to these variables using the equal sign. For example, you might set the starting score to zero and the number of lives to three. Remember, you only use “var” when creating a new variable. A common mistake is to use it every time you refer to a variable, which is unnecessary.
Often, you’ll want to create a variable and give it a value right away. You can do this in one line of code, a process called initializing the variable. This line of code does two things: it creates the variable and assigns it a value.
In programming, the equal sign is known as the assignment operator because it assigns a value to a variable. Unlike in math, where the equal sign shows equality, in programming, it sets a variable to a specific value. For example, if you write “score = 5” and then “score = 7,” the computer will first set the score to five and then change it to seven.
To avoid confusion, programmers often say “gets the value” instead of “equals.” So, “score = 7” can be read as “score gets the value seven.” Some programming languages even use a sideways arrow to show this assignment.
To use a variable, you simply refer to it by its label. Wherever the variable name appears in your program, the computer retrieves its current value from memory. However, you can’t always see the value directly. To check a variable’s value, you can use commands like “write” or “console.log” to display it, which is a handy trick.
Remember, there’s a difference between showing a variable’s value and writing out its label. If you put text in quotes, the computer will display those exact characters. Without quotes, the computer assumes you’re referring to a variable and tries to retrieve its value. If the variable doesn’t exist, you’ll get an error.
Explore a simple game or app and identify at least three variables that might be used in its programming. Think about what information changes as you play, like scores or levels. Write down your findings and discuss how these variables might be initialized and updated during the game.
Create a list of ten different scenarios where variables could be used, such as tracking the number of students in a class or the temperature in a room. For each scenario, come up with a descriptive and meaningful variable name. Share your list with a partner and explain your choices.
Write a short program that uses at least three variables to store different types of information, like a name, age, and favorite color. Then, intentionally introduce a mistake, such as using “var” again when updating a variable. Exchange programs with a classmate and try to find and fix each other’s errors.
Create a short story or comic strip where variables are the main characters. Describe how they change throughout the story and how the assignment operator plays a role in their adventures. Present your story to the class and explain the programming concepts illustrated in your narrative.
Design a simple quiz using a programming language that allows you to create and update variables. Include questions that require the user to input answers, and use variables to keep track of the score. Share your quiz with classmates and see who can achieve the highest score.
Here’s a sanitized version of the provided YouTube transcript:
—
Most apps keep track of pieces of information that change and update as you use the app. For example, a simple game can keep track of your score and the number of lives you have left as you play. Apps utilize a computer’s memory to do this, and learning how to use that memory when programming is a really powerful skill.
The most common way to leverage a computer’s memory is by creating a variable. A variable is just a container for storing a value, and once created, the variable is stored in the computer’s memory and can be used repeatedly throughout your program.
As an example, let’s use variables to keep track of the score and the number of lives remaining in a hypothetical game. Creating the variables is really easy. You use the word “var” followed by a label that you want to give the variable. You can choose any label you want, but it helps to choose a word that describes the variable you want to store. So let’s label our first variable “score” to keep track of the score, and then we can create a second variable to keep track of lives.
Remember these labels; you’ll need them later in order to use the variables. We can assign a value to the variable using the equal sign. Let’s set our starting score to zero and our number of lives to three. Remember, you only need to use “var” at the time that you want to create a new variable. A common mistake is to use it every single time you want to refer to a variable. Also, be sure to avoid mixing up the order. When you call your variables, the labels have to appear on the left, and the values have to appear on the right.
Often, you’ll want to create a variable and assign it a value right away. There’s a really convenient way to do this with one line of code. This is sometimes called initializing the variable. It’s important to realize that this single line of code is actually doing two things: first, creating the variable, and then second, assigning it a value.
Let’s take a moment to talk about the equal sign. In many programming languages, the equal symbol is called the assignment operator because you use it to assign a value to a variable. You’re probably used to seeing it in math, but in programming, it’s a little bit different. If you were in math class and you saw this, something would definitely be wrong. A score can’t possibly equal both five and seven, but this is not math class with code. A computer is executing a set of instructions one after the other. So this would just tell the computer to set the score to five, and then on the next line, set it to seven.
In an effort to avoid confusion, programmers often don’t use the word “equals” when we see this sign. Instead, they use the phrase “gets the value.” So when we see a statement like this, we could read it as “score gets the value seven” or “score gets seven” for short. Some programming languages avoid the confusion entirely by representing the assignment statements with a sideways arrow.
To use a variable, you just refer to it by its label. Anywhere the variable name appears in the program, the value currently stored in the variable will be retrieved from memory. The tricky part is that when using a variable, you often can’t see visually what its value is. To bypass that problem, you can use “write” or “console.log” to display the value. This is a really handy trick.
But remember, there’s a difference between showing the value of a variable and writing out the variable’s label. When text appears in quotes, you’re telling the computer to write those literal characters on the screen. On the other hand, without quotes, the computer assumes that you’re referring to a variable called “score,” and it will attempt to retrieve its value. If you haven’t created that variable, then the computer will give an error.
—
This version maintains the original content while ensuring clarity and coherence.
Variables – Containers in programming that store data values which can change during the execution of a program. – In our game, we used variables to keep track of the player’s score and remaining lives.
Programming – The process of designing and building an executable computer program to accomplish a specific computing task. – Programming allows us to create fun and interactive games on the computer.
Memory – The component of a computer that stores data and instructions for quick access by the processor. – The game runs smoothly because it efficiently uses the computer’s memory to load graphics and sounds.
Score – A numerical representation of a player’s success in a game, often stored as a variable. – The player’s score increased by 10 points each time they collected a coin.
Lives – A count of the number of attempts a player has before the game ends, often represented as a variable. – The player lost one of their lives when they hit an obstacle.
Equal – A comparison operator in programming that checks if two values are the same. – The program checks if the player’s score is equal to the high score to display a congratulatory message.
Value – The data stored in a variable, which can be a number, text, or other data types. – The value of the score variable was updated each time the player completed a level.
Label – A descriptive name given to a variable or function to indicate its purpose in the program. – We used the label “playerHealth” for the variable that tracks the player’s remaining lives.
Initialize – To assign an initial value to a variable before it is used in a program. – Before the game starts, we initialize the score variable to zero.
Retrieve – To access and obtain the value stored in a variable or memory location. – The program retrieves the player’s high score from memory each time the game loads.