| Previous | Table of Contents | Next |
![]() | On line 4, the four variables are declared and initialized. Their values are printed on lines 5 and 6. On line 7, a is assigned the value 9. On line 8, b is assigned the value 7. On line 9, the values of a and b are summed and the result is assigned to x. This expression (x = a+b) evaluates to a value (the sum of a + b), and that value is in turn assigned to y. |
![]() | An operator is a symbol that causes the compiler to take an action. |
![]() | The assignment operator (=) causes the operand on the left side of the assignment operator to have its value changed to the value on the right side of the assignment operator. The expression |
x = a + b;
assigns the value that is the result of adding a and b to the operand x.
An operand that can legally be on the left side of an assignment operator is called an l-value. That which can be on the right side is called (you guessed it) an r-value.
Constants are r-values; they cannot be l-values. Thus, you can write
x = 35; // ok
but you cant legally write
35 = x; // error, not an l-value!
![]() | An l-value is an operand that can be on the left side of an expression. An r-value is an operand that can be on the right side of an expression. Note that all l-values are r-values, but not all r-values are l-values. An example of an r-value that is not an l-value is a literal. Thus, you can write x = 5;, but you cannot write 5 = x;. |
There are five mathematical operators: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Addition, subtraction, and multiplication act pretty much as you might expect. Not so with division.
Integer division is somewhat different from everyday division. When you divide 21 by 4, the result is a real number (a number with a fraction). Integers dont have fractions, and so the remainder is lopped off. The value returned by 21 / 4 is 5.
The modulus operator (%) returns the remainder value of integer division. Thus 21 % 4 is 1, because 21 / 4 is 5 with a remainder of 1.
Surprisingly, finding the modulus can be very useful. For example, you might want to print a statement on every 10th action.
It turns out that any number % 10 will return 0 if the number is a multiple of 10. Thus 20%10 is zero. 30%10 is zero.
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 harder to read is
myAge += 2;
The self-assigned addition operator (+=) adds the r-value to the l-value and then reassigns the result into the l-value. This operator is pronounced plus-equals. The statement would be read myAge plus-equals two. If myAge had the value 4 to start, it would have 6 after this statement.
There are self-assigned subtraction (-=), division (/=), multiplication (*=), and modulus (%=) operators as well.
![]() | The most common value to add (or subtract) and then reassign into a variable is 1. In C++ increasing a value by 1 is called incrementing, and decreasing by 1 is called decrementing. There are special operators to perform these actions. |
The increment operator (++) increases the value of the variable by 1, and the decrement operator () decreases it by 1. Thus, if you have a variable, c, and you want to increment it, you would use this statement:
C++; // Start with C and increment it.
This statement is equivalent to the more verbose statement
C = C + 1;
which you learned is also equivalent to the moderately verbose statement
C += 1;
![]() | Both the increment operator (++) and the decrement operator () come in two flavors: prefix and postfix. The prefix variety is written before the variable name (++myAge); the postfix variety, after (myAge++). |
In a simple statement, it doesnt much matter which you use, but in a complex statement, when you are incrementing (or decrementing) a variable and then assigning the result to another variable, it matters very much. The prefix operator is evaluated before the assignment; the postfix is evaluated after.
The semantics of prefix is this: increment the value and then fetch it. The semantics of postfix is different: fetch the value and then increment the original.This can be confusing at first, but if x is an integer whose value is 5 and you write
int a = ++x;
you have told the compiler to increment x (making it 6) and then fetch that value and assign it to a. Thus a is now 6 and x is now 6.
If, after doing this, you write:
int b = x++;
| Previous | Table of Contents | Next |