Objective-C 2.0 Operator Precedence

From Techotopia

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) formatObjective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

In the previous chapter of Objective-C 2.0 Essentials we looked at Objective-C operators and expressions. An equally important area to understand is operator precedence. This is essentially the order in which Objective-C evaluates expressions comprising more than one operator.

When humans evaluate expressions, they usually do so starting at the left of the expression and working towards the right. For example, working from left to right we get a result of 300 from the following expression:

10 + 20 * 10 = 300

This is because we, as humans, add 10 to 20, resulting in 30 and then multiply that by 10 to arrive at 300. Ask Objective-C to perform the same calculation and you get a very different answer:

When executed, the result of the calculation is assigned to integer variable x and subsequently displayed using the NSLog call:

2009-10-09 10:37:07.856 t[3511] Result is 210

As we can see from the above output, Objective-C considers the answer to be 210. This is a direct result of operator precedence. Objective-C has a set of rules that tell it in which order operators should be evaluated in an expression. Clearly, Objective-C considers the multiplication operator (*) to be of a higher precedence than the addition (+) operator.

When addressing the issue of operator precedence in some scripting and programming languages, all that is generally required is a table listing the operators in order of precedence from highest to lowest. Objective-C has more in common with languages such as Java and C# in that operators are grouped together at different precedence levels. When operators from the same precedence level are found within the context of a single expression, a rule as to the order in which the operators are to be evaluated is followed. This rule is referred to as the associativity and differs from one group to the next. The following table outlines the operator precedence groups and corresponding associativity for Objective-C:

Operator

Description

Precedence

Associativity

[].()->

access array element or message expression
access object member or method
invoke a method or function
pointer to structure member

As we can see from the table, when operators from the same precedence level appear in an expression, the operators are evaluated either left to right or right to left depending on the associativity of that group. For example, the following expression will be evaluated left to right because both operators are in the same precedence level and this is the rule dictated by the corresponding associativity:

The precedence and associativity rules built into Objective-C can be overridden by surrounding the lower priority section of an expression with parentheses. For example, we can override precedence and force a left to right evaluation of our original example as follows:

int x = (10 + 20) * 10;
NSLog(@"Result is %i", x);

In the above example, the expression fragment enclosed in parentheses is evaluated before the higher precedence multiplication resulting in the expression equaling 300 instead of the 210 result we saw when we allowed precedence to take effect: