| Previous | Table of Contents | Next |
you have now told the compiler to fetch the value in x (6) and assign it to b, and then go back and increment x. Thus, b is now 6 but x is now 7. Listing 4.2 shows the use and implications of both types.
Listing 4.2 Demonstrates Prefix and Postfix Operators
1: // Listing 4.2 - demonstrates use of
2: // prefix and postfix increment and
3: // decrement operators
4: #include <iostream.h>
5: int main()
6: {
7: int myAge = 39; // initialize two integers
8: int yourAge = 39;
9: cout << I am:\t << myAge << \tyears old.\n;
10: cout << You are:\t << yourAge << \tyears old\n;
11: myAge++; // postfix increment
12: ++yourAge; // prefix increment
13: cout << One year passes...\n;
14: cout << I am:\t << myAge << \tyears old.\n;
15: cout << You are:\t << yourAge << \tyears old\n;
16: cout << Another year passes\n;
17: cout << I am:\t << myAge++ << \tyears old.\n;
18: cout << You are:\t << ++yourAge << \tyears old\n;
19: cout << Lets print it again.\n;
20: cout << I am:\t << myAge << \tyears old.\n;
21: cout << You are:\t << yourAge << \tyears old\n;
22: return 0;
23: }
![]() | I am 39 years old You are 39 years old One year passes I am 40 years old You are 40 years old Another year passes I am 40 years old You are 41 years old Lets print it again I am 41 years old You are 41 years old |
![]() | On lines 7 and 8 two integer variables are declared, and each is initialized with the value 39. Their value is printed on lines 9 and 10. |
On line 11 myAge is incremented using the postfix increment operator, and on line 12 yourAge is incremented using the prefix increment operator. The results are printed on lines 14 and 15, and they are identical (both 40).
On line 17, myAge is incremented as part of the printing statement, using the postfix increment operator. Because it is postfix, the increment happens after the print, and so the value 40 is printed again. In contrast, on line 18 yourAge is incremented using the prefix increment operator. Thus, it is incremented before being printed, and the value displays as 41.
Finally, on lines 20 and 21 the values are printed again. Because the increment statement has completed, the value in myAge is now 41, as is the value in yourAge.
In the complex statement
x = 5 + 3 * 8;
which is performed first, the addition or the multiplication? If the addition is performed first, the answer is 8 * 8, or 64. If the multiplication is performed first, the answer is 5 + 24, or 29.
![]() | Every operator has a precedence value, and the complete list is shown in Appendix A, Operator Precedence. Multiplication has higher precedence than addition, and thus the value of the expression is 29. |
When two mathematical operators have the same precedence, they are performed in left-to-right order. Thus
x = 5 + 3 + 8 * 9 + 6 * 4;
is evaluated multiplication first, left to right. Thus, 8*9 = 72, and 6*4 = 24. Now the expression is essentially
x = 5 + 3 + 72 + 24;
Now the addition, left to right, is 5 + 3 = 8; 8 + 72 = 80; 80 + 24 = 104.
Be careful with this. Some operators, such as assignment, are evaluated in right-to-left order! In any case, what if the precedence order doesnt meet your needs? Consider the expression
TotalSeconds = NumMinutesToThink + NumMinutesToType * 60
In this expression you do not want to multiply the NumMinutesToType variable by 60 and then add it to NumMinutesToThink. You want to add the two variables to get the total number of minutes, and then you want to multiply that number by 60 to get the total seconds.
In this case you use parentheses to change the precedence order. Items in parentheses are evaluated at a higher precedence than any of the mathematical operators. Thus
TotalSeconds = (NumMinutesToThink + NumMinutesToType) * 60
will accomplish what you want.
For complex expressions you might need to nest parentheses one within another. For example, you might need to compute the total seconds and then compute the total number of people who are involved before multiplying seconds times people:
TotalPersonSeconds = ( ( (NumMinutesToThink + NumMinutesToType) * 60) ⇒ * (PeopleInTheOffice + PeopleOnVacation) )
This complicated expression is read from the inside out. First, NumMinutesToThink is added to NumMinutesToType, because these are in the innermost parentheses. Then this sum is multiplied by 60. Next, PeopleInTheOffice is added to PeopleOnVacation. Finally, the total number of people found is multiplied by the total number of seconds.
This example raises an important issue. This expression is easy for a computer to understand, but very difficult for a human to read, understand, or modify. Here is the same expression rewritten, using some temporary integer variables:
TotalMinutes = NumMinutesToThink + NumMinutesToType; TotalSeconds = TotalMinutes * 60; TotalPeople = PeopleInTheOffice + PeopleOnVacation; TotalPersonSeconds = TotalPeople * TotalSeconds;
This example takes longer to write and uses more temporary variables than the preceding example, but it is far easier to understand. Add a comment at the top to explain what this code does, and change the 60 to a symbolic constant. You then will have code that is easy to understand and maintain.
In previous versions of C++ all truth and falsity were represented by integers, but the new ISO/ANSI standard has introduced a new type: bool. This new type has two possible values, false or true.
Every expression can be evaluated for its truth or falsity. Expressions which evaluate mathematically to zero will return false, all others will return true.
![]() |
|
| Previous | Table of Contents | Next |