Remembering Stuff With Variables
Pssst...use your keyboard arrows to turn the page!
In the last few chapters, we’ve learned how to do some amazing things with code. Our programs can now repeat the same commands as long as we want them to, and we’ve given them the power to make decisions using if-then statements!
In this chapter, we’re going to learn another amazing superpower: how to help our computer programs remember things.
Computer programs need to remember things all the time. For example, think about when you start a new video game. Most of the time, you’ll be asked to give the player character a name. Then, as you play the game, the game will sometimes need to use the name you gave it earlier.
But can you imagine if the game couldn’t remember the name you gave it? It would have to ask you what your name is each time it wanted to use it. And that would get awkward.
Thankfully, we can use things called variables to hold onto information like our character name. A variable is basically just a box. We give the variable box a descriptive title, like “Character Name”. When we enter our character’s name, that name is stored safely inside of the “Character Name” box.
Now, whenever our program needs to use our character name, it can just go to the variable box, check the information inside of it, and then use that information as it needs to.
Variables can be used to hold onto all sorts of information. Computer programs use variables to remember how many points you’ve scored, or how much health you have left, or what your password is.
Now, take 30 seconds to think of as many other uses for variables as you can with your class!