Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

2/24/2013

A closer look at operator precedence

Classic definition of order of precedence from the Wiki is, as follows:
In mathematics and computer programming, the order of operations (sometimes called operator precedence) is a rule used to clarify which procedures should be performed first in a given mathematical expression

You can find this and similar definitions in all beginner programming books, along with tables that shows it graphically.
Here is a one such table. I like it because it is compressed, and it shows all important operators in C, grouped by types related precedence. It is not so visually nice, but it will do the job. There are more detailed tables, specific to different languages (  for example here you can't find dot ( "." ) operator from the OOP paradigm ...), but I think that we  should first learn the basics:


() [] -> .
! ~ ++ -- + - * & sizeof
* / %
+ -
<< >>
< <= >= >
== !=
&
^
|
&&
||
?:
= += -= *= /= %= &= ^= |= <<= >>=
,

Closely related with precedence is associativity  - which describes how operators of the same precedence are grouped in the absence of parentheses. There is left or right associativity.


Now that you know the basics, there are some interesting things that can be learned by looking in this table.

For example why operator of assignment has a lowest priority ? And why in OOP paradigm operator dot ( "." ) has highest priority ?
That's because dot operator is potentially capable to give one of the highest level of abstraction to data, compared to all other operators. What does it mean ?

Well,  if we declare and define object in javascript like this:


// object literal
var obj = {};

You are actually providing opportunity to program to move further from basic  values which are building blocks of programs. As we know, all expression must be evaluated before usage. So if you have expression in classic math:
 5 +  10 * 20 
We know that we are talking about evaluation which leads to some result.

Same thing is happening in programming. All expressions needs to be evaluated to some basic values, in order to work with them. Lets see example in javacript:


//object literal with one property
var obj= {

    prop: "Hello Operators"

    }

If we want to access obj prop property, we'll have to use our dot (".") operator. Why ? Because prop is abstracted or moved away more from the language that computer understand.
So it needs to be evaluated to some basic ground, before it can be used.

I am talking here about built in data types in most popular  languages, which serves as a point of evaluation. On a deeper level, everything gets translated to binary, but that is not important for this topic.

What about assignment operator ( = ) ? Well, in table it has one of the lowest priority. Why is that ? Assignment is directly connected with memory in most imperative style programming languages. In fact, when you assign a value to some variable, using assignment operator, you are creating a memory address and a space for that varible on a machine level. That variable is carrying a value of some basic type. We can say that assignment operator is standing last in line, waiting all operations to be done, before it can assign a value to some name. If obj.prop appears somewhere else in a code for example:



// conditional statement that evaluates  property value
if ( obj.prop) {

    // do something

}




It will again be evaluated to it's actual value ( "Hello Operators" string ).

In this post I looked at two extreme positions in a table of operator precedence. By comparing their behaviour, we can see why they have different precedence, and also what is happening with all other operators. They  appear everywhere in language, and they are connective tissue of values. So it is important to be familiar with them more then it can be found in classic examples. Here I tried to fill some gaps, which were missing for me to completely understand them.

I hope it will help you also.

Happy Coding.







No comments:

Post a Comment