Operator Precedence

You may recall from high school arithmetic that an expression like 5 + 3 * 9, is calculated as though it were written 5 + (3 * 9), not (5 + 3) * 9; that is, you have to perform the * before the +, so that the correct result is 32, not 72, as it would be under the latter interpretation. The reason for performing the operations in the former order is that multiplication has a higher precedence than addition, which is just another way of saying that multiplication is performed before addition in the same expression. Well, every operator in C++ also has a precedence that determines the order of application of each operator in an expression with more than one operator. This seems like a good idea at first glance, since after all, arithmetic does follow precedence rules like the one we just saw. Unfortunately, C++ is just a little more complicated than arithmetic, and so its precedence rules are not as simple and easy to remember as those of arithmetic. In fact, there are 17 different levels of precedence, which no one can remember. Therefore, everyone (or at least, everyone who is sensible) ends up using parentheses to specify what order was meant when the expression was written; of course, if we're going to have to use parentheses, then why do we need precedence rules in the first place?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset