# Operators Precedence in Scala

An operator is a symbol that represents an operation to be performed with one or more operand. Operators are the foundation of any programming language. Which operator is performed first in an expression with more than one operators with different precedence is determined by operator precedence.

For example, 10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10 + 20) * 30.
when two operators of the same precedence appear in expression associativity is used. Associativity can be Right to Left or Left to Right. For example ‘*’ and ‘/’ have the same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is worked as “(100 / 10) * 10”.

In below table operators with the highest precedence appear at the top of the table and operators with the lowest precedence appear at the bottom.

Operator

Category

Associativity

()[] Postfix

Left to Right

! ~ Unary Right to Left
* / % Multiplicative Left to Right
+ – Additive Left to Right
>> >>> << Shift Left to Right
< <= > >= Relational Left to Right
== != Relational is equal to/is not equal to Left to Right
== != Equality Left to Right
& Bitwise AND Left to Right
^ Bitwise exclusive OR Left to Right
| Bitwise inclusive OR Left to Right
&& Logical AND Left to Right
| | Logical OR Left to Right
= += -= *= /= %= >>= <<= &= ^= |= Assignment Right to left

,

Comma (separate expressions) Left to Right

Below is the example of operator Precedence.

Example :

 `// Scala program to show Operators Precedence ` ` `  `// Creating object ` `object` `gfg ` `{ ` `    ``// Main method ` `    ``def` `main(args``:` `Array[String]) ` `    ``{ ` `     `  `        ``var` `a``:``Int ``=` `20``; ` `        ``var` `b``:``Int ``=` `10``; ` `        ``var` `c``:``Int ``=` `15``; ` `        ``var` `d``:``Int ``=` `5``; ` `        ``var` `e ``=` `0` `         `  `        ``// operators with the highest precedence ` `        ``// will operate first ` `        ``e ``=` `a + b * c / d;  ` `             `  `        ``/* step 1: 20 + (10 * 15) /5 ` `            ``step 2: 20 + (150 /5) ` `        ``step 3:(20 + 30)*/` `             `  `        ``println(``"Value of a + b * c / d is : "` `+ e ) ` `     `  `    ``} ` `} `

Output:

`Value of a + b * c / d is : 50`

In above example e = a + b * c / d; here, e is assigned 50, not 120 because operator * has a higher precedence than / than +, so it first gets multiplied with 10 * 15, then divide by 5 and then add into 20.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.