Variables

Filed under , on Sunday, January 31, 2010

0

When an application is to handle data, it needs some place where it can temporarily store this data. This “place” where the application stores data is called the memory.
An application will handle more than one piece of data. Thus, the application has to allocate memory for each piece of data. In allocating memory there are two factors to be considered:
•    How much memory is to be allocated ?
•    Remembering where in memory is each piece of data stored.
Earlier programmers had to write their programs in the language of the machine that is, in 1s and 0s. If the programmer wanted to temporarily store a value, the exact storage locations inside the memory of the computer had to be assigned. This storage location had to be a specific number or memory address.
Modern day languages enable us to use symbolic names known as variables, to refer to the memory location where a particular value is to be stored.
The type of data decides the amount of memory to be allocated. The names that we assign to variables to store a particular data help us in retrieving this data as and when required.
We are familiar with the use of letters, which represent quantities in a formula. For example, the area of a rectangle is given by:
    Area = A = Length x Breadth = L x B
    The simple interest is given by:
    Interest = I = Principal x Time x Rate / 100 = P x T x R / 100
The letters A, L, B, I, P, T and R are all variables and are short notations that represent various values.
To appreciate the significance of variables, consider the following example:
    The sum of the marks obtained by 5 students is to be displayed.
    The sum can be displayed with the help of the following instruction.
    Display the sum of 24, 56, 72, 36 and 82.
Once the sum is displayed, it is lost from the computer’s memory. Suppose we want to calculate the average of the marks, the sum would have to be recalculated.
A better approach will be to store the result in the computer’s memory, and retrieve it as and when required.
    sum = 24 + 56 + 72 + 36 + 82
Here, sum is a variable that is used to store the sum of the five numbers. To calculate average, it can be done as follows:

    Avg = sum/5
Programs can use many memory locations represented by variable names.

Variable Name  
The rules for naming variables are different for each programming language. However, some conventions that are typically followed are:
•    Variable names must begin with alphabet.
•    The first character may be followed by a sequence of letters or digits and can also include special character ‘underscore’.
•    Avoid using the letter O in situations where it can be confused with the number 0 and similarly the lowercase letter l can be mistaken with the number 1. For example, names like polo, logo should be avoided.
•    Typically, uppercase and lowercase letters are treated as different. For instance, the variable ADD, add and Add are all different. Although some programming languages do not differ them.
•    The name of the variable should be descriptive of the value it holds. For example, if the sum of two numbers is to be found, the variable containing the result can be named sum. Naming it s or ab12 is not a good idea as these could stand for just about anything.

Counter Variable   
One of the variables used frequently when writing programs involving loops is the counter variable. This variable keeps track of the number of times a loop has been performed.
It is initially assigned a value of zero, which increases as and when a loop is performed. We usually name this variable as cnt.
The following examples give an idea of how variables are to be named and used.
Example   Enter any 5 numbers and count the odd and even numbers entered.

 Figure 1

In this example, we consider three variables cnt, odd and even. Before we could read any numbers the three variables are set to zero.
Given below is the description of each of these variables.
•    Cnt: Is the counter variable. It keeps a count of the numbers entered that is, the number of times the loop is to be performed. The value is initially set to 0. Every time a number is read the value in the counter increases by 1. The instant it is five the program terminates.
•    Odd: Is the variable required to keep track if the number entered is odd. The value is zero initially, increasing each time an odd number is entered.
•    Even: Is the variable required to keep track if the number entered is even. The value is zero initially, increasing each time an even number is entered.

Comments Posted (0)

Post a Comment