Also note that to override this associativity, put parentheses
around the expression as follows:
total = x * (y/z);
This expression now computes (y/z) first, multiplies x by that value, and assigns the resultant
value to ‘total’ (the variable).
Sometimes we do not need the default precedence to produce the
desired result. For example,
total = x + y * z;
Here, since the multiplication operator has a higher precedence over addition, the expression
calculates y*z first and then adds this value to x. On the other hand, if you wish to compute x+y first
and then multiply that result by z, use parentheses as follows:
total = (x + y) * z;
The rule can be stated as follows: “Parentheses are required whenever you want to override
associativity as the default precedence. But within a pair of parentheses, the same hierarchy (as
Do'stlaringiz bilan baham: |