4. Variables

X Becomes X Plus 3

Let us now try something a little "scarier":

double x = 4;
x = x+3;

What is the value of x? Does the second statement even make sense? It does if we understand the symbol '=' denotes the operation of assignment in C++. To perform the assignment, the value on its right needs to be calculated first.

The value of variable x is set to 4 as the consequence of the first statement. The second statement first makes us evaluate the value on the right side of the assignment. Since x equals 4 at that moment, the expression x+3 evaluates to value 7. This value is then assigned to variable x. Thus, the second statement set value of variable x to 7. In other words, the second statement increases the value of variable x by 3.