August 2001
Beginner
482 pages
10h 52m
English
It is not uncommon to want to add a value to a variable and then to assign the result back into the variable. If you have a variable myAge and you want to increase the value by two, you can write
int myAge = 5; int temp; temp = myAge + 2; // add 5 + 2 and put it in temp myAge = temp; // put it back in myAge
This method, however, is terribly convoluted and wasteful. In C++ you can put the same variable on both sides of the assignment operator, and thus the preceding becomes
myAge = myAge + 2;
which is much better. In algebra this expression would be meaningless, but in C++ it is read as “add two to the value in myAge and assign the result to myAge.”
Even simpler to write, but perhaps a bit ...
Read now
Unlock full access