Variables

Variables

Definition
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

  1. Can contain letters and numbers, and must start with a letter:
    1. OK Names:  temperature, outsideTemperature1, outsideTemperature2
    2. NOT OK: 3outsideTemperature, name@
  2. Should describe the data the variable holds
    1. OK: firstName, score
    2. Not OK: a, t1 , someRandomJunk

Data Types

  1. Different kinds of information take up different amounts of space in memory.
  2. There are two categories of simple data - numbers and Strings
    1. Strings are words, or anything that you cannot do a number calculation on
  3. A data type is the kind of information a variable will hold.
  4. Some languages, like Java and Python need to know the data type before you can use the variable.
  5. Other languages like Scratch decide what the data type is when you store the first value in it
 

Example Program

  1. What data type is the timesTen variable? Number or String?
  2. If you needed to store a phone number, should it be a number or String? (Hint:  would you ever need to divide or add a phone number?)
  3. What happens if you type in a word like cat instead of a number?
  4. How could you help students understand the similarities and differences between variables in math, science and computer science?


Initializing Variables

In programs sometimes it helps to set a variable to a starting value. This is called initializing. In the Sample Scratch Project for ASCII the variable alpha is initialized to "ABCDEFGHIJKLMNOPQRSTUVWXYZ". This lets the program pull out a single letter from the alphabet.

Lesson on input and variables from CodeVA's Coaches Academy.


    • Related Articles

    • Overview: Algorithms and Programming

      Context This strand is at the core of how computer science gets done. Programming is how humans give instructions to the computer, directions are encoded into commands that the computer follows. Everything a computer does is encoded. This idea of ...
    • Tools of Programming

      Link to Printable PDF When students begin to program, especially in block-based languages, it is easy for them to be engrossed with all of the features. As they explore it is important to both cultivate their excitement for creation and expression, ...
    • What is Computer Science?

      Defining Computer Science Computer science moves beyond using technology tools towards an understanding of how they work and ultimately designing new solutions to enduring human problems. Despite common misperceptions, computer science is not simply ...
    • ASCII

      Computer science has lots invisible systems at work behind the scenes. These are the rules for how computer programs work, data is stored, and how devices will communicate over networks. One rule about computers and programming is the need to create ...
    • Computer Science Endorsement in Virginia

      There are several routes to licensure that enable teachers to teach computer science courses. Additionally, teachers may be able to teach computer science classes with existing endorsements, depending on the course code the course is assigned by the ...