11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3: Performing Mathematical Operations 43This expression has the same effect as dividing x by 132.The original expressionx / 100 + 32is identical to the expression(x/100) + 32In a given expression, C++ normally performs multiplication and division beforeaddition or subtraction. Multiplication and division have higher precedencethan addition and subtraction.In summary: Precedence refers to the order in which operators are evaluated.An operator with higher precedence is executed first. You can override theprecedence of an operator by using parentheses.Performing Unary OperationsArithmetic binary operators — those operators that take two arguments —are familiar to a lot of us from school days. You’ve probably been doing binaryoperations since the first grade in school. But consider the unary operators,which take a single argument (for example, –a). Many unary operations arenot so well known.The unary mathematical operators are plus, plus-plus, minus, and minus-minus(respectively, +, –, ++, and – –). Thusint var1 = 10;int var2 = -var1;The latter expression uses the minus unary operator (–) to calculate the valuenegative 10.The minus operator changes the sign of its argument. Positive numbers becomenegative and vice versa. The plus operator does not change the sign of itsargument. It wouldn’t be weird to say the plus operator has no effect at all.The ++ and the – – operators might be new to you. These operators (respectively)add one to their arguments or subtract one from their arguments, sothey’re known (also respectively) as the increment and decrement operators.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!