<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Simon Whatley &#187; commutative law</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/commutative-law/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Operator Precedence in ColdFusion</title>
		<link>http://www.simonwhatley.co.uk/operator-precedence-in-coldfusion</link>
		<comments>http://www.simonwhatley.co.uk/operator-precedence-in-coldfusion#comments</comments>
		<pubDate>Wed, 05 Mar 2008 10:47:42 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[associative law]]></category>
		<category><![CDATA[associativity]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[commutative law]]></category>
		<category><![CDATA[distributive law]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[order]]></category>
		<category><![CDATA[precedence]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=445</guid>
		<description><![CDATA[In arithmetic and algebra, when a number or expression is both preceded and followed by a binary operation (a calculation involving two operands), a rule is required for which operation should be applied first. From the earliest use of mathematical notation, multiplication took precedence over addition, whichever side of a number it appeared on. Thus 3 + 4 — 5 = 5 — 4 + 3 = 23.]]></description>
			<content:encoded><![CDATA[<p>In arithmetic and algebra, when a number or expression is both preceded and followed by a binary operation (a calculation involving two operands), a rule is required for which operation should be applied first. From the earliest use of mathematical notation, multiplication took precedence over addition, whichever side of a number it appeared on. Thus 3 + 4 — 5 = 5 — 4 + 3 = 23.</p>
<p>Operator precedence, therefore, determines the order in which operators are evaluated in a statement. Operators with higher precedence are evaluated first.</p>
<p>A simple example can be expressed as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;"><span style="color: #0000ff">x</span> <span style="color: #0000ff">=</span> <span style="color: #ff0000;">3</span> <span style="color: #0000ff;">+</span> <span style="color: #ff0000;">4</span> * <span style="color: #ff0000;">5</span>;</span>
<span style="color: #000099;"><span style="color: #800080;">writeOutput</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff">x</span><span style="color: #000000;">&#41;</span>; <span style="color: #808080;">//returns 23</span></span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>This is equivalent to wrapping the <code>4 * 5</code> in parentheses:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;"><span style="color: #0000ff">x</span> <span style="color: #0000ff">=</span> <span style="color: #ff0000;">3</span> <span style="color: #0000ff;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #ff0000;">4</span> * <span style="color: #ff0000;">5</span><span style="color: #000000;">&#41;</span>;</span>
<span style="color: #000099;"><span style="color: #800080;">writeOutput</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff">x</span><span style="color: #000000;">&#41;</span>; <span style="color: #808080;">//returns 23</span></span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>If we switched the parentheses, the result returned would be entirely different:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #000099;"><span style="color: #800000;">&lt;cfscript&gt;</span></span>
<span style="color: #000099;"><span style="color: #0000ff">x</span> <span style="color: #0000ff">=</span> <span style="color: #000000;">&#40;</span><span style="color: #ff0000;">3</span> <span style="color: #0000ff;">+</span> <span style="color: #ff0000;">4</span><span style="color: #000000;">&#41;</span> * <span style="color: #ff0000;">5</span>;</span>
<span style="color: #000099;"><span style="color: #800080;">writeOutput</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff">x</span><span style="color: #000000;">&#41;</span>; <span style="color: #808080;">//returns 35</span></span>
<span style="color: #000099;"><span style="color: #800000;">&lt;/cfscript&gt;</span></span></pre></div></div>

<p>This is an important point to note. Operators have an order of precedence, but this can be overridden using parentheses.  Statements within parentheses are always evaluated first, before moving on to the outer statements.</p>
<p><strong>Associativity</strong></p>
<p>The concept of Associativity determines the order in which operators of the same precedence are processed. For example, consider the following expression (where OP stands for order of precedence):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">a OP b OP c</pre></div></div>

<p>Left-associativity (left-to-right) means that it is processed as:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">(a OP b) OP c</pre></div></div>

<p>Right-associativity (right-to-left) means it is interpreted as:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">a OP (b OP c)</pre></div></div>

<p>The following table details operators order of precedence:</p>
<table>
<thead>
<tr>
<th>Precedence</th>
<th>Operator type</th>
<th>Associativity</th>
<th>Individual operators</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">1</td>
<td>increment</td>
<td>n/a</td>
<td>++</td>
</tr>
<tr>
<td>decrement</td>
<td>n/a</td>
<td>&#8211;</td>
</tr>
<tr>
<td rowspan="3">2</td>
<td>logical-not</td>
<td>right-to-left</td>
<td>!</td>
</tr>
<tr>
<td>unary +</td>
<td>right-to-left</td>
<td>+</td>
</tr>
<tr>
<td>unary negation</td>
<td>right-to-left</td>
<td>-</td>
</tr>
<tr>
<td rowspan="3">3</td>
<td>multiplication</td>
<td>left-to-right</td>
<td>*</td>
</tr>
<tr>
<td>division</td>
<td>left-to-right</td>
<td>/</td>
</tr>
<tr>
<td>modulus</td>
<td>left-to-right</td>
<td>%</td>
</tr>
<tr>
<td rowspan="2">4</td>
<td>addition</td>
<td>left-to-right</td>
<td>+</td>
</tr>
<tr>
<td>subtraction</td>
<td>left-to-right</td>
<td>-</td>
</tr>
<tr>
<td rowspan="4">5</td>
<td rowspan="4">relational</td>
<td rowspan="4">left-to-right</td>
<td>&lt;</td>
</tr>
<tr>
<td>&lt;=</td>
</tr>
<tr>
<td>&gt;</td>
</tr>
<tr>
<td>&gt;=</td>
</tr>
<tr>
<td rowspan="2">6</td>
<td rowspan="2">equality</td>
<td rowspan="2">left-to-right</td>
<td>==</td>
</tr>
<tr>
<td>!=</td>
</tr>
<tr>
<td>7</td>
<td>logical-and</td>
<td>left-to-right</td>
<td>&amp;&amp;</td>
</tr>
<tr>
<td>8</td>
<td>logical-or</td>
<td>left-to-right</td>
<td>||</td>
</tr>
<tr>
<td rowspan="7">9</td>
<td rowspan="7">assignment</td>
<td rowspan="7">right-to-left</td>
<td>=</td>
</tr>
<tr>
<td>+=</td>
</tr>
<tr>
<td>-=</td>
</tr>
<tr>
<td>*=</td>
</tr>
<tr>
<td>/=</td>
</tr>
<tr>
<td>%=</td>
</tr>
<tr>
<td>&amp;=</td>
</tr>
</tbody>
</table>
<p><strong>Summary</strong></p>
<p>Warning: Multiplication and division are of equal precedence, and addition and subtraction are of equal precedence. Using any of the above rules in the order addition first, subtraction afterward would give the wrong answer to</p>
<p><code>10 - 3 + 2</code></p>
<p>The correct answer is 9, which is best understood by thinking of the problem as the sum of positive ten, negative three, and positive two.</p>
<p><code>10 + (-3) + 2</code></p>
<p>It is usual, wherever you need to calculate operations of equal precedence to work from left to right. The following rules of thumb are useful:</p>
<p>First: perform any calculations inside parentheses (brackets)</p>
<p>Second: Next perform all multiplication and division, working from left to right</p>
<p>Third: Lastly perform all addition and subtraction, working from left to right</p>
<p>However, with experience, the <a href="http://en.wikipedia.org/wiki/Commutative_law" title="commutative law" rel="nofollow">commutative law</a>, <a href="http://en.wikipedia.org/wiki/Associative _law" title="associative law" rel="nofollow">associative law</a>, and <a href="http://en.wikipedia.org/wiki/Distributive_law" title="distributive law" rel="nofollow">distributive law</a> allow shortcuts. For example,</p>
<p><code>17 x 24 / 12</code></p>
<p>is much easier when worked from right to left, where here the answer is 34.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/operator-precedence-in-coldfusion/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

