3
For more : http://comsciguide.blogspot.com/2015/06/associativity-of-operators.html Associativity of operators : In general, expressions are the combination of operands and operators. An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We have different types of operators which have some priority. According to priority, the expressions are simplified. For example : a*b+c; Here, operands are a,b,c and operators are *, + From the table, we see that * have high priority than +. So the expression is equal to ((a*b) +c).This tells that the * operator is executed first and + next. Suppose if an expression contains two or more equal priority operators, then we have to use the Associativity of operators. For example : a * b + c / d - e operands are a,b,c,d,e operators are * , + , / , -

Associativity of operators

Embed Size (px)

Citation preview

Page 1: Associativity of operators

For more : http://comsciguide.blogspot.com/2015/06/associativity-of-operators.html

Associativity of operators :

In general, expressions are the combination of operands and operators.

An operator is a symbol that tells the compiler to perform specific

mathematical or logical manipulations. We have different types of

operators which have some priority. According to priority, the

expressions are simplified.

For example :

a*b+c;

Here, operands are a,b,c and operators are *, +

From the table, we see that * have high priority than +. So the

expression is equal to ((a*b) +c).This tells that the * operator is

executed first and + next.

Suppose if an expression contains two or more equal priority operators,

then we have to use the Associativity of operators.

For example :

a * b + c / d - e

operands are a,b,c,d,e operators are * , + , / , -

Page 2: Associativity of operators

For more : http://comsciguide.blogspot.com/2015/06/associativity-of-operators.html

From the priority table we get * and / have equal priority and also +

and – have equal priority.

We get the expression as (a * b) + ( c / d) - e

Now the problem arises which one in the bracket should first executed.

(a * b) or (c / d) and ((a * b) + (c / d)) or ((c / d) - e) i.e.

either + or - .

Associativity indicates in which order two operators of same

precedence (priority) executes first.

For example :

Suppose if we consider the expression a == b != c ,

Operands are a,b,c and operators are == , != .These two have same

priority. The associativity of both == and != is left to right i.e. the

expression in left is executed first and execution takes place towards

right. Thus a == b != c is equal to ((a == b) != c).

Page 3: Associativity of operators

For more : http://comsciguide.blogspot.com/2015/06/associativity-of-operators.html

Now if we consider the above expression , As arithmetic operators

have left to right precedence, expression which is on the left is solved

first i.e. ((a * b) + (c / d)) and final expression is (((a * b ) + ( c / d )) - e)