Tuesday, January 14, 2014

Variable names, such as number1, number2 and sum, actually correspond to locations
in the computer’s memory. Every variable has a name, a type, a size and a value.
     In the addition program in Fig. 3.11, the statement (line 26)

                number1 = Int32.Parse( firstNumber );

converts to an int the string that the user entered. This int is placed into a memory loca-
tion to which the name number1 has been assigned by the compiler. Suppose the user en-
ters the string 45 as the value for firstNumber. The program converts firstNumber
to an int, and the computer places the integer value 45 into location number1, as shown
in Fig. 3.12.
     When a value is placed in a memory location, this value replaces the previous value in
that location. The previous value is lost (or destroyed).

     When the statement (line 27)

              number2 = Int32.Parse( secondNumber );

executes, suppose the user types 72 as the value for secondNumber. The program con-
verts secondNumber to an int, the computer places the integer value 72 into location
number2 and memory appears as shown in Fig.3.13.
     Once  the  program has obtained values for number1 and  number2, it adds these
values and places their total into variable sum. The statement

               sum = number1 + number2;

performs the addition and replaces (i.e., destroys) sum’s previous value. After calculating
the sum, memory appears as shown in Fig. 3.14. Note that the values of number1 and
number2 appear exactly as they did before the calculation of sum. These values were
used, but not destroyed, as the computer performed the calculation. Thus, when a value is
read from a memory location, the process is nondestructive.


0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!