Objective-C 2.0 Operator Precedence

From Techotopia
Revision as of 15:05, 9 October 2009 by Neil (Talk | contribs) (New page: 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 essent...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.


Contents


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

+ -
+

additive
string concatenation

5 left to right

<< >>
>>>

shift

6 left to right
<  <=
>  >=

instanceof

relational
type comparison

7 left to right

==
!=

equality

8 left to right
& bitwise AND 9 left to right
^ bitwise XOR 10 left to right
| bitwise OR 11 left to right
&& conditional AND 12 left to right
|| conditional OR 13 left to right
?: conditional 14 right to left
  =   +=   -=
 *=   /=   %=
 &=   ^=   |=
<<=  >>= >>>=

assignment

15 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