Operator precedence with brackets
Understanding operator precedence when brackets are used.
Learning Objectives
- Operator precedence with the use of brackets.
Source Code
|
Run Output
Code Understanding
int a=5,b=7,c=9; //Three integer variables initialised
int d= a++ + (b%(c%a)) + (a+b+c);
Above precedence problem would be solved in steps.
STEP 1: 5 + (7%(9%6)) + (6+7+9)
Post fix will be applied first. second and third instance of postfix will become 6, first instance will remain 5.
STEP 2: 5 + (7%(3)) + (22)
Internal most brackets shall be solved
STEP 3: 5 + 1 + 22
Next level of brackets shall be solved. Each bracket set will follow operator precedence of its own.
STEP 4: 28
Final computation done when it comes down to + – level
System.out.println(d);
Output printed
Notes
- The operator precedence table can be seen at this link.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Common Errors
- Student often try to do make these evaluations in one go and errors are quite likely. The stepwise approach is always better.
Suggested Filename(s): OperatorPrecedenceBrackets.java
sunmitra| Created: 5-Mar-2018 | Updated: 5-Mar-2018|