I l@ve RuBoard |
![]() ![]() |
5.12 Operators for Performing ShortcutsC++ not only provides you with a rich set of declarations, but also gives you a large number of special-purpose operators. Frequently a programmer wants to increment (add 1 to) a variable. Using a normal assignment statement, this would look like: total_entries = total_entries + 1; C++ provides you a shorthand for performing this common task. The ++ operator is used for incrementing: ++total_entries; A similar operator, -- , can be used for decrementing (subtracting 1 from) a variable. --number_left; // Is the same as number_left = number_left - 1; But suppose you want to add 2 instead of 1. Then you can use the following notation: total_entries += 2; This is equivalent to: total_entries = total_entries + 2; Each of the simple operators shown in Table 5-4 can be used in this manner.
![]() |
I l@ve RuBoard |
![]() ![]() |