back


Compound Operators

C++ uses several special operators called compound operators. These operators provide a shortcut to writing out longer statements such as:
	x = x + 5

C++ allows you to say the same thing by typing:
x += 5
You don't absolutely have to use the compound operators, they just make your job easier by giving you less to type. Here's a table:
compound operator	Example		the Long Way
+= x += 4 x = x + 4 -= x-=9 x = x - 9 *= y *= 8 y = y * 8 /= z /= 7 z = z / 2 %= r %= 4 r = r % 4
You do need to be careful when using compound operators because the order of precedence is less for them (see page 75 and Appendix B of your Knowlton book).

Assignment Write a program that will calculate the sales tax(8%)for an item given by the user. Store the answer in a variable named the_tax. Use compound operators to solve the problem.