Objective-C 2.0 Operator Precedence
Previous | Table of Contents | Next |
Objective-C Operators and Expressions | Commenting Objective-C Code |
Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format Objective-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.
An Example of Objective-C Operator Precedence
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:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int x = 10 + 20 * 10; NSLog(@"Result is %i", x); [pool drain]; return 0; }
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.
Objective-C Operator Precedence and Associativity
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 |
Highest | left to right |
++ |
increment |
right to left | |
* |
multiply |
left to right | |
+ |
add |
left to right | |
<< |
bitwise shift left |
left to right | |
<= |
less than or equal to |
left to right | |
== |
equality inequality |
left to right | |
& | bitwise AND | left to right | |
^ | bitwise XOR | left to right | |
| | bitwise OR | left to right | |
&& | logical AND | left to right | |
|| | logical OR | left to right | |
?: | conditional | right to left | |
= += -= *= /= %= &= ^= |= <<= >>= >>>= |
assignment |
right to left | |
, |
comma |
Lowest | right to left |
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:
int x = 10 * 20 / 5;
Overriding Operator Precedence
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:
2009-10-09 11:42:43.845 t[5630] Result is 300
<google>BUY_OBJC_BOTTOM</google>
Previous | Table of Contents | Next |
Objective-C Operators and Expressions | Commenting Objective-C Code |