when we run a program the computer executes each instruction line by line then when it finishes with an instruction it clears out its working memory so the computer has forgotten what it just did by the time it gets to the next line but what if we want to remember the result of a calculation across lines we need a way to tell the computer hey remember the result of this instruction this time I'm going to need this value later to do that we use variables a variable is a reserved location in the computer's memory for storing
a value we associate each location with a name so it's easy for us as the programmers to refer to a specific location later we can also update the value that a specific name points to over time because we have a name we can ask the computer questions like what value do you have stored at location view count right now to create a new variable or update the value stored in an existing variable we'll use an assignment statement assignment statement starts with the name of the variable on the left hand side then an equal sign also
called the assignment operator and on the right hand side the value to be stored we read this statement as the Variable View count is assigned the value 324 when the computer executes this instruction it'll go off to its short-term memory and look for a label with the name view count if it doesn't find one it'll set aside or allocate a new chunk of its memory and then label that location with with the name view count finally it'll store the value on the right hand side of the assignment operator in that location if it does find
that label it will allocate a new chunk of memory store the new value there and take that label from the old location and move it to point to the new location that old location is now unreachable there's no way for you as the programmer to ask about that location in memory anymore because it doesn't have a name there's no need for the computer to keep this around so it just erases it anytime we want to act access the value stored at a location we can ask the computer to retrieve it by giving it the name
for example if I had the instruction print view count the computer would go find the memory location view count grab the value stored there and substitute it in so this would print 324 remember instructions in a program execute in order so the value stored in a certain variable might be different at different points in the program for example after the first line executes the location view count contains the value 324 but after the third line executes the location view count now contains the value 501 so when I access view count on line two this prints
324 but when I access view count on line 4 this prints 51 note that after line 4 when the program terminates the computer clears out all these locations in memory that means the next time you run the program the computer starts fresh again you can think of variables like min whiteboards when you create a new variable you go off and get a new whiteboard you give that whiteboard a name and then you write a value on it to store it there you can create 100 variables which means you went off and got 100 different whiteboards
and if you want to know what value is stored on a specific whiteboard named color theme then you look for the label color theme grab that whiteboard and look at the value written on it if you have a second or third assignment statement for the same variable color theme the computer doesn't go off and grab a new whiteboard cuz then it would have two whiteboards labeled color theme and that's confusing instead it grabs the existing whiteboard name color theme erases the old value on the front and then in its place writes down the new value
when the program terminates we erase all the whiteboards peel the labels off the back and put them neatly back on the Shelf where we found them so they're nice and clean for the next program execution to use how might we use variables in a real program say we're building a game what data might we need to keep track of well we might need to remember the player current score what level they're on and what character they selected as the program runs we'll need to update these values over time like maybe when the player collects an
item their score increases by five if we want to be able to ask the computer what's the player's current score at any point in the program we need to tell the computer to store that value in a variable some games might have hundreds or thousands of different pieces of data that they need to keep track of which means hundreds or thousands of different variables think closely about a piece of technology use every day like an app or a game or an appliance in your house what data is it keeping track of what variables might it
have as you learn to think like a programmer you'll start seeing variables everywhere