Reader Level:
Articles

Precedence and Associativity of Arithmetic Operators in TypeScript

By Sharad Gupta on November 21, 2012
In this article I am going to explain how to work precedence and associativity in arithmetic operators.
  • 0
  • 0
  • 2995

Arithmetic Operators Precedence and Associativity in TypeScript

Precedence

If we talk about precedence of the Arithmetic operators, then you will see that primary operators are the highest precedence and then unary operators have precedence and the last are binary operators with the lowest precedence.

Associativity

If we talk about precedence of the multiplication and division operators then you will find, these operators contain the same precedence. Now let's see an example.

(a) 50/5*2 = 20.
(b) 50/5*2 = 5.

Then, what is corect result?

How can TypeScript determine which operator is applied first? The answer to this question is Associativity. An operator can either have left-right or right-left associativity. When any binary operators with the same precedence is applied with the same operand, then left-right associativity is applied. And if any unary and primary operator with the same precedence is applied with the same operand, then right-left associativity will be applied.

The given table tells you which operator has higher precedence and how to associativity on them. In this table precedence is given in decreasing order in other words precedence category one (1) has the highest priority.


Precedence Category   Symbol   Name Associativity
1 Primary  (<Numeric_expression>)
++(Postfix)
 --(Postfix) 
Paraenthesis
Increment operator
Decrement operator 
Right-Left
Right-Left
Right-Left
2 Unary +
-
--(Pretfix)
++(Pretfix)
Unary  plus
Unary  minus
Decrement operator
Increment operator
Right-Left
Right-Left
Right-Left
Right-Left
3
Binary
 
*
/
%
Multiplication
Division
Modulus
Left-Right
Left-Right
Left-Right
4 Binary +
-
Addition
Subtraction
Left-Right
Left-Right


Do the following steps to create a program using Precedence and Associativity in Arithmetic Operators.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". After that, a window is opened, enter the name of your application like "ArithmeticPrecedenceNAssociativity", then click on the Ok button.


Step 2

After Step 1 your project has been created. Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, css file and html files.
 

Step 3

The code for the Arithmetic Operators Precedence and Associativity program:

ArithmeticPrecedenceNAssociativity.ts

 

class ArithmeticPrecedenceNAssociativity {

    Myfunction() {

        var PreRes: number = 100 + 5 * 10;// Precedence

        document.write("Precedence res=" + PreRes + "<br/>");

        var AssoRes: number = 100 / 5 * 2;//Associativity

        document.write("Associativity res=" + AssoRes);

    }

}

window.onload = () =>{

    var call = new ArithmeticPrecedenceNAssociativity();

    call.Myfunction();
}


Note: In the above declared program, multiplication operation is performed first, because the multiplication operator has greater precedence than the addition operator. And in the associativity part, two operators (such as (/) and (*)) with the same precedence as the same operand, left-to-right associativity will cause the left-most operator to be applied first, in other words the divide operation is performed first and then the multiplication operation is performed.

default.html
 

<!DOCTYPEhtml>

 

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>TypeScript HTML App</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

 

    <divid="content"/>

</body>

</html>


app.js

 

var ArithmeticPrecedenceNAssociativity = (function () {

    function ArithmeticPrecedenceNAssociativity() { }

    ArithmeticPrecedenceNAssociativity.prototype.Myfunction = function () {

        var PreRes = 100 + 5 * 10;

        document.write("Precedence res=" + PreRes + "<br/>");

        var AssoRes = 100 / 5 * 2;

        document.write("Associativity res=" + AssoRes);

    };

    return ArithmeticPrecedenceNAssociativity;

})();

window.onload = function () {

    var call = new ArithmeticPrecedenceNAssociativity();

    call.Myfunction();
};

 

Step 4

Output

precendence-associativity-in-typescript.jpg

Sharad Gupta

Hi I am a Software developer working with different Microsoft technologies like ASP.NET 3.5/4.0, AJAX, JavaScript, J Query, Ms-access, SOL Server 2005/2008/2014,ADO.NET,Typescript also enamoured to learn different Micros... Read more

COMMENT USING

Trending up