Computers store information in binary
. A series of 1's and 0's represent numbers, letters, pictures, and everything else the computer stores. The computer tracks all of these locations so that the data can be retrieved when it is needed. Just like we find buildings on a map, the computer assigns addresses to the locations where information is stored.
This is one of those processes that is happening in the background when you use a computer that you are not even aware of. It is also similar to how information is retrieved on the Internet.
When we create programs, we often need to store and recall information. Picture a program that asks the person using it to type in their name. The computer has to encode the letters into binary, then find an empty spot in memory big enough to holds all of those ones and zeros, then record the address of where the name is stored so that it can be found and used later.
Fortunately, programming languages take care of these details for us. As the programmer, our job is to create algorithms, not remember the addresses and size of the data the computer is storing for us.
So instead of using the memory addresses, a programmer uses variables. A variable is a name that represents data stored in memory. To ask the user their name we might use the variables firstName and lastName. We can then use input commands to ask them to enter the name.
While the program is running the variable's value can change. When the program is done running the values entered are lost, unless they are moved to a more permanent type of memory like a text file.
Gotcha - For students the word variable can be tricky, since it is also used in math and science.
Rules for Variable Names
- Can contain letters and numbers, and must start with a letter:
- OK Names: temperature, outsideTemperature1, outsideTemperature2
- NOT OK: 3outsideTemperature, name@
- Should describe the data the variable holds
- OK: firstName, score
- Not OK: a, t1 , someRandomJunk
- Different kinds of information take up different amounts of space in memory.
- There are two categories of simple data - numbers and Strings
- Strings are words, or anything that you cannot do a number calculation on
- A data type is the kind of information a variable will hold.
- Some languages, like Java and Python need to know the data type before you can use the variable.
- Other languages like Scratch decide what the data type is when you store the first value in it