Section 5 - Playing Computer
2A.5.1 Picturing Memory With Assignment Statements
If you have never programmed before, you'll need a mental picture of what happens when you write an assignment statement like:
age = 21;
In the computer's memory, there is a location labeled age, and this statement causes a 21 to be placed inside the location. Here is a picture to help. The upper portion of the picture shows your line of code as it would appear in a program you might write. The lower portion of the picture is the mental image you should have, representing the computer memory after the statement is executed:

As you can see, the assignment of 21 to age, (in the statement age = 21;) should evoke a picture of 21 being placed in the age location. It is a bit like placing mail in a mailbox. age is the name on the outside of the mailbox, and 21 is a letter placed in the box. The only problem with that metaphor is that you can place lots of letters in a mailbox but you can only place ONE ITEM in a memory location.
Let's say, a little further down in your program, you have a new statement, age = 15; This assignment will completely overwrite what was in the age location and replace it with 15. Here's the before and after picture:

Because age cannot contain more than one value, the 15 overwrites the 21. This is important.
Every year, one or two students make the following mistake. They place two assignment statements, back-to-back, assigning two values to the same variable:
age = 21; age = 15; // horrible - makes no sense
Now that you have the picture in memory, you know that this makes absolutely no sense. Why would we put a 21 in age if we are going to immediately destroy it in the very next statement with a 15? If there were intervening statements between these two assignments, then it would be perfectly fine:
age = 21; cout << "My age is " << age << endl; age = 15; // now this is okay
Because the programmer actually did something with the old age before overwriting it with the new age, it makes sense to have these statements.
2A.5.2 Variables on the LHS and RHS of the = Operator
Often, one or more variables will appear on the Right Hand Side (RHS) of the assignment operator (=). For example, here is a typical assignment statement that does this:
myAge = herAge + 3;
We know we are going to put a number into the "mailbox" or memory location myAge. But before we do that, we have to figure out what value to assign. The answer is on the RHS of the assignment operator. The expression herAge + 3 means to take the value stored in herAge, add 3 to it, and that's what you put into myAge.
Here is your picture. Make sure you understand it before going on:

When a variable or object appears on the RHS, we take its current value and use that value. We are reading what is inside it. When a variable appears on the Left Hand Side (LHS) of the assignment operator, we are storing a new value into it, overwriting what was there.
Now, this is nice, and we understand how to picture variables that appear on the right (read it) or left (write to it) of the assignment operator. But what if the same variable appears on both sides?
xVal = xVal - 4;
This is a very important statement to understand, even now, in the first few days of the class. We must get 100% comprehension, so let's have a mental image. The xVal is playing a different role on the RHS than it is playing on the LHS. On the RHS we are reading the old value and using that for the computation. That happens first. After that, on the LHS we are writing the result, erasing the old value.
Let's assume that xVal happened to have the value 3 stored in it (from earlier in the program) when we encounter this statement. Here's the statement followed by the mental picture:

We took the 3out of the location so we could work with it. It's the old value, but we need to use that old value to compute with. Next, we subtract 4 from the value, which leaves us with -1. Finally, we store the -1 back into the same location, xVal. xVal now has -1. The old value, 3, is gone forever.
2A.5.3 Playing Computer
What we did above is a simple technique anyone can do with a pencil and paper. It gives us a way to look at computer programs and predict what is happening at each line of the code. We can draw out all the variables on a piece of paper as soon as we are presented with a program. Let's consider the start of a very simplistic program:
main() { int age, xVal, temp; age = 91; temp = age - 8; age = temp + 19; // etc. }
When trying to simulate what happens internally in memory as this program runs, we look at the first line of a program source:
int age, xVal, temp;
This is the declaration of the three variables, age, xVal and temp,. We spring into action. We grab a pencil and paper and write the variable locations without putting anything into them yet:

Next, we move to the first statement.
age = 91;
This tells us to write a 91 into the location age. So we do it.

We continue this, line-by-line. After the next two lines,
temp = age - 8; age = temp + 19;
our paper would look like this:

This is called "playing computer." You'll have to do this when you get stuck. It will reveal the errors in your logic, and this leads to ideas on how to fix those errors.