An Introduction to Programming: Variables
Created | Updated Jan 28, 2002
Before we can really look at what variables are, we have to understand about data, information, and most importantly the distinction between data and information.
The difference between Data and Information
This is very clear in a complex way, it goes something like this:
A) "Unhappyness" is data, it tells you something, but means nothing without context.
B) "Your flat mate is unhappy with you" is information, this is because it has a
context and so meaning.
C) "Your flat mate has changed the locks and written that your an idiot over the
front of the house." This is information, and also implys by it's context the information that your flat
mate has attained levels of unhappyness with you only previously known by victims of tactical nuclear
strikes.
A more technical example is this1, imagine you need to store 20 seperate variables that just happen to hold the same number[e.g 42]. There are two ways to store it, 1) have 20 memory spaces holding the number or 2) Have one memory space saying there are 20 variables all with the same value. Now method 1 takes more data than method 2 but they both hold the same amount of information.
Now we understand the nature of data we can begin to look at what variables are, now primarily
they are data, but the way in which they are used in the program means they have context and
so can be information. There are data structures that are by definition really information structures
2, but we aren't looking at these here. Next we will look at what variables, as pieces of data, mean to programmers.
Variables from the Perspective of Programmers
Variables can be anything looked at in the right way, a name,
an age, the number of milk bottles you got last autumn, the
fractal pattern of a ferns leaves, anything.
They are in effect what a program deals with, the sources of raw data about something.
Now in a way this is a simplistic view, because as programmers when we decide on a variable, we can't just tell the computer to set up a variable for "the number of milk bottles you got last autumn". As the computer will ask3 "What is a number?", "What is Autumn?", "What do you mean by last?", "What is a milk bottle?", and "What is the point of all those weird little collections of symbols in between?".
In effect initially (e.g when you initialise4 the variable) you have to give a variable a name (To which the program associates a memory location in compilation), a variable type (This defines the nature of the information being stored in the memory location, a number, a Character, a whatever), and (usually) an initial value.
Arrays, a special type of variable
Now this has an unique nature, in respect of how it is viewed by a programmer, in so far as it is a group of variables of a certain type, treated as one variable with several parts.
Now this has to be looked at simply as a series of memory locations, that are numbered, but associated with a single name. For example an array called Fred is set up with an initial size of a memory locations. To access each location within Fred, we use the format Fred[n]. Where n is the memory locations number within Fred. Now the maximum value of n is a-1 as the first memory location is numbered 0.
Memory location 0 | Memory location 1 | Memory location 2 | Memory location 3 | Memory location 4 |
Character arrays, are also called character strings, and they hold multiple characters as 1 item, like a name, or a sentance
H | E | L | L | O | End of Line |
Numeric arrays are usually used to hold a series of numbers that are related together. For example a series of results from an experiment, the series of outputs of a volt meter recorded, say, at different times during the experiment.
1 | 125 | 7 | 554 | 45 |
Variables from the Perspective of Computers
Bits5 and memory addresses, they are used by the computer, altered, moved,
translated to other codes, but they have no meaning to the computer. They only have
meaning to the programmer and users of the program, when viewed from
interfaces6.
Now as I said the output of programs is information as they have context, but it
would be wrong to think this means anything to the computer.
To explain this the following bit of psuedocode7:
LINE 1) integer theanswer;
LINE 2) theanswer=42;
LINE 3) print "The answer to the ultimate question of life the universe and everything, is..is.. ";
LINE 4) print theanswer;
Now this will print on the screen:
The answer to the ultimate question of life the universe and everything, is..is.. 42
Now this is information as it has data (theanswer) in a context, but it doesn't mean
anything to the computer other than the following:
LINE 1) Link variable identity to a memory location.
LINE 2) Transferre the binary number 101010 [42 in decimal] to memory location.
LINE 3) Transferre the binary code for the string "The answer to the ultimate question of life
the universe and everything, is..is.." to the screen.
LINE 4) Transferre contents of memory location to screen.
Now this is a very simplified version but the principle is right.
Variable Types
The following is a list of common types of variable, and a definition [For how to manipulate variables see Variable Manipulation].
Integer
Whole numbers, either Positive or Negative.
Examples: -5,-4,-3,-2,-1,0,1,2,3,4,5
Float
Numbers with decimals, either Positive or Negative.
Examples: -5.454,-4.567,-3.2,-2.248,-1.1,0,1.287,2.683,3.798,4.598,5.67821
Character
A single character.
Examples: a, B, G, I, l, y8
Character String
A String of characters, basically characters held in a 1 dimensional array.
Example of Character Strings9:
H | i | t | c | h | SPACE | H | i | k | e | r | END OF LINE |
Boolean
This is the simplest data type of all, Boolean means it is either True or False. Now this makes it useful in logical operations, as the outcome of any logical operation10 is either True or False.
Constants
It has been said there are very few things in life that are constant other than death and taxes.
But in programming, constants are variables that are set and then don't vary in the course of
the programs operation. It is also odd in so far as your constant has to be a constant variable
of one of the previous variable types (e.g. const int variable_name;11)
Uses of Variables
Variables can be used in many different ways in a program, here are a few standard examples, but in effect the possibilities are only limited by imagination:
Read in/write out Variables [Any Type]
These are variables that are used as the inputs and outputs of the program, think of carts carrying data into and out of the program.
Counters [Numeric Variables]
These are used to count up or count down with loops, they are often used as part of the condition (e.g.
end the loop when the counter variable has reached 100)
Sentinel Values [Any Type]
This means a variables value is used for true or false, it is the sentry, if it's value is correct
(It meets the set down value of the condition) the sentry will allow you to pass.
Intermediate Data [any type]
Used for holding data that is significant to the working of the program. (e.g. Refresh screen every 0.5 seconds, or how many seconds to wait).
Related Entries
Programming Entries
An Introduction to ProgrammingLanguage Specific Entries
Variables in C++distributions or Windows, are graphical interfaces. There are also audio interfaces like sound effects in a computer game, and also printout interfaces, for example when a report is generated it automatically gives you a hard copy of it.7Practice code, gives program
methods but isn't syntaxically correct.8It is good to notice that the commas, spaces, and returns can
also count as characters, as they are treated by the computer like characters.9I have replaced unseen characters with italic descriptions.
10Examples of Logical Operations: Variable a AND Variable b, Variable a OR Variable b, NOT Variable a11This defines a
constant of the variable type integer, called variable_name