For example if I want to know the average of two grades, We can write “average = (grade_l + grade_2) / 2. 0;” and the variable average will then contain the average grade regardless of the scores stored in the variables, grade_l and grade_2. There are only a few things you can do with a variable: . Create one (with a nice name). A variable should be named to represent all possible values that it might contain. Some examples are: midterm_score, midterm_scores, data_points, course_name, etc. 2.
Don’t waste your time!
Order your assignment!
Put some information into it (destroying whatever was there before). We “put” information into a variable using the assignment operator, e. G. , midterm_score = 93; 3. Get a copy of the information out of it (leaving a copy inside) We “get” the information out by simply writing the name of the variable, the computer does the rest for us, e. G. , average = (grade_l + grade_2) / 2. Variable Properties There are 6 properties associated with a variable. The first three are very important as you start to program.
The last three are important as you advance and improve your skills (and the complexity of the programs you are creating). 1. Name 2. Type 3. Value Clarification of Properties 1. Name The name is Symbolic. It represents the “title” of the information that is being stored with the variable. The name is perhaps the most important property to the programmer, because this is how we “access” the variable. Every variable must have a unique name 2. Type The type represents what “kind” of data is stored with the variable. . Value A variable, by its very name, changes over time. Thus if the variable is Jims_age and is assigned the value 21 . At another point, Jims_age may be assigned the value 27. Most of the time, when you “create a variable” you are primarily defining the variables name and type. Often (and the best programming practice) you will want to provide an initial value to be associated with that variable name. If you forget to assign an initial value, then various rules “kick in” depending on the language. Legal Variable