34,333
edits
Changes
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...
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:
<pre>
#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;
}
</pre>
When executed, the result of the calculation is assigned to integer variable ''x'' and subsequently displayed using the NSLog call:
<pre>
2009-10-09 10:37:07.856 t[3511] Result is 210
</pre>
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:
<table cellspacing="1" cellpadding="5" border="0">
<tr align = center bgcolor = "#666666">
<th>Operator</th>
<th>Description</th>
<th>Precedence</th>
<th>Associativity</th>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>[]</tt><br>
<tt> .</tt><br>
<tt>()</tt><br>
<tt>-></tt><br>
</td>
<td>
access array element or message expression<br>
access object member or method<br>
invoke a method or function<br>
Pointer to structure member<br>
</td>
<td>Highest</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>++</tt><br>
<tt>--</tt><br>
<tt>+</tt><br>
<tt>-</tt><br>
<tt>!</tt><br>
<tt>~</tt><br>
<tt>*</tt><br>
<tt>&</tt><br>
<tt>sizeof</tt><br>
<tt>(type)</tt>
</td>
<td>
increment<br>
decrement<br>
unary plus<br>
unary minus<br>
logical NOT<br>
ones complement<br>
pointer reference<br>
address of<br>
size of object<br>
type cast
</td>
<td></td>
<td>right to left</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>*</tt><br>
<tt>/</tt><br>
<tt>%</tt>
</td>
<td>
multiply<br>
divide<br>
modulus<br>
</td>
<td></td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>+ -</tt><br>
<tt>+</tt>
</td>
<td>
additive<br>
string concatenation
</td>
<td>5</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt><< >></tt><br>
<tt>>>></tt>
</td>
<td>
shift
</td>
<td>6</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<pre>
< <=
> >=
</pre>
<tt>instanceof</tt>
</td>
<td>
relational<br>
type comparison
</td>
<td>7</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>==</tt><br>
<tt>!=</tt>
</td>
<td>
equality
</td>
<td>8</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>&</tt></td>
<td>bitwise AND</td>
<td>9</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>^</tt></td>
<td>bitwise XOR</td>
<td>10</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>|</tt></td>
<td>bitwise OR</td>
<td>11</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>&&</tt></td>
<td>conditional AND</td>
<td>12</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>||</tt></td>
<td>conditional OR</td>
<td>13</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>?:</tt></td>
<td>conditional</td>
<td>14</td>
<td>right to left</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<pre>
= += -=
*= /= %=
&= ^= |=
<<= >>= >>>=
</pre>
</td>
<td align>
assignment
</td>
<td>15</td>
<td>right to left</td>
</tr>
</table>
== Overriding Operator Precedence ==
The precedence built into Ruby can be overridden by surrounding the lower priority section of an expression with parentheses. For example:
<pre>
(10 + 20) * 10
=> 300
</pre>
In the above example, the expression fragment enclosed in parentheses is evaluated before the higher precedence multiplication.
== Operator Precedence Table ==
== 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:
<pre>
#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;
}
</pre>
When executed, the result of the calculation is assigned to integer variable ''x'' and subsequently displayed using the NSLog call:
<pre>
2009-10-09 10:37:07.856 t[3511] Result is 210
</pre>
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:
<table cellspacing="1" cellpadding="5" border="0">
<tr align = center bgcolor = "#666666">
<th>Operator</th>
<th>Description</th>
<th>Precedence</th>
<th>Associativity</th>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>[]</tt><br>
<tt> .</tt><br>
<tt>()</tt><br>
<tt>-></tt><br>
</td>
<td>
access array element or message expression<br>
access object member or method<br>
invoke a method or function<br>
Pointer to structure member<br>
</td>
<td>Highest</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>++</tt><br>
<tt>--</tt><br>
<tt>+</tt><br>
<tt>-</tt><br>
<tt>!</tt><br>
<tt>~</tt><br>
<tt>*</tt><br>
<tt>&</tt><br>
<tt>sizeof</tt><br>
<tt>(type)</tt>
</td>
<td>
increment<br>
decrement<br>
unary plus<br>
unary minus<br>
logical NOT<br>
ones complement<br>
pointer reference<br>
address of<br>
size of object<br>
type cast
</td>
<td></td>
<td>right to left</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>*</tt><br>
<tt>/</tt><br>
<tt>%</tt>
</td>
<td>
multiply<br>
divide<br>
modulus<br>
</td>
<td></td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>+ -</tt><br>
<tt>+</tt>
</td>
<td>
additive<br>
string concatenation
</td>
<td>5</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt><< >></tt><br>
<tt>>>></tt>
</td>
<td>
shift
</td>
<td>6</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<pre>
< <=
> >=
</pre>
<tt>instanceof</tt>
</td>
<td>
relational<br>
type comparison
</td>
<td>7</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<tt>==</tt><br>
<tt>!=</tt>
</td>
<td>
equality
</td>
<td>8</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>&</tt></td>
<td>bitwise AND</td>
<td>9</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>^</tt></td>
<td>bitwise XOR</td>
<td>10</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>|</tt></td>
<td>bitwise OR</td>
<td>11</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>&&</tt></td>
<td>conditional AND</td>
<td>12</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>||</tt></td>
<td>conditional OR</td>
<td>13</td>
<td>left to right</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td><tt>?:</tt></td>
<td>conditional</td>
<td>14</td>
<td>right to left</td>
</tr>
<tr align = center bgcolor = "#ebebeb">
<td>
<pre>
= += -=
*= /= %=
&= ^= |=
<<= >>= >>>=
</pre>
</td>
<td align>
assignment
</td>
<td>15</td>
<td>right to left</td>
</tr>
</table>
== Overriding Operator Precedence ==
The precedence built into Ruby can be overridden by surrounding the lower priority section of an expression with parentheses. For example:
<pre>
(10 + 20) * 10
=> 300
</pre>
In the above example, the expression fragment enclosed in parentheses is evaluated before the higher precedence multiplication.
== Operator Precedence Table ==


