Objective-C 2.0 Operator Precedence

Revision as of 15:33, 9 October 2009 by Neil (Talk | contribs) (Objective-C Operator Precedence and Associativity)

Revision as of 15:33, 9 October 2009 by Neil (Talk | contribs) (Objective-C Operator Precedence and Associativity)

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 on 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

Highest left to right

++
--
+
-
!
~
*
&
sizeof
(type)

increment
decrement
unary plus
unary minus
logical NOT
ones complement
pointer reference
address of
size of object
type cast

right to left

*
/
%

multiply
divide
modulus

left to right

+
-

add
subtract

left to right

<
>
<

left shift right shift less than

left to right

<=
>=
>

less than or equal to
greater than or equal to
greater than

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

Overriding Operator Precedence

The precedence built into Ruby can be overridden by surrounding the lower priority section of an expression with parentheses. For example:

(10 + 20) * 10
=> 300

In the above example, the expression fragment enclosed in parentheses is evaluated before the higher precedence multiplication.

Operator Precedence Table