Variables: Overview

Home

Computer programs process information. Some of the information that is input, stored, and used in a computer program has a value that is constant, meaning it does not change throughout the course of the program. An example of a constant in math is ‘pi’ because ‘pi’ has one value that never changes.

Other pieces of information have values that vary or change during the running of a program. Programmers create variables to hold the value of information that may change. In a game program, a variable may be created to hold the player’s current score, since that value would change (hopefully!) during the course of the game.

What some pieces of information in your daily life that are constants and others that are variables.

When I create or declare a variable I give it a value. During the course of my program I can read the value out of that variable and also write a new value into that variable.

The Variable Game

  1. We need three volunteers.
  2. Person 1 & 2 you are going to play the role of variables. As we know variables hold some value.
  3. Class pick a number between 1 and 5. Lets assign that value to Person 1 to remember for us.
  4. Class pick a shape (circle, square or triangle). Lets assign that value to Person 2 to rember for us.
  5. Finally, person 3, you are our computer program. Your job is to get the number value from our first variable, and our shape value from our second variable, and draw that on the board. However, keep in mind the program cannot remember anything, it just knows the steps to complete, the program needs to ask the varables for their values every time it runs.
  6. Lets run our program.
  7. Now lets update our number variable, and run our program again.