Skip to content

Commit 8dc7db8

Browse files
committed
Added precedence table and examples
1 parent acf4622 commit 8dc7db8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lessons/images/precedence_java.jpg

57.2 KB
Loading

lessons/operators.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,29 @@ public class Main {
214214
## **Operators Precedence**
215215
216216
Operators Precedence is the order in which the operators are evaluated. Below is the precedence of the operators.
217+
![precedence](./images/precedence_java.jpg)
218+
219+
```java
220+
public class Main {
221+
public static void main(String[] args) {
222+
int a = 2, b = 10, c = 0, d = 40, e = 40, f = 30;
223+
224+
// precedence rules for arithmetic operators.
225+
// (* = / = %) > (+ = -)
226+
// prints a+(b/d)
227+
System.out.println("a+b/d = " + (a + b / d));
228+
229+
// if same precendence then associative
230+
// rules are followed (evaluation occurs by the order
231+
//in which the operators are present from left to right)
232+
// e/f -> b*d -> a+(b*d) -> a+(b*d)-(e/f)
233+
System.out.println("a+b*d-e/f = "
234+
+ (a + b * d - e / f));
235+
}
236+
}
237+
```
238+
Output:
239+
```
240+
a+b/d = 2
241+
a+b*d-e/f = 401
242+
```

0 commit comments

Comments
 (0)