Operator precedence with brackets – Computer Sir Ki Class


Code Learning #JAVA#3738 siteicon   siteicon   siteicon  

Operator precedence with brackets

Understanding operator precedence when brackets are used.

Learning Objectives

  • Operator precedence with the use of brackets.

Source Code

TC++ #3738

Source Code

class OperatorPrecedenceBrackets
public static void main(String[] args)
int a=5,b=7,c=9;
int d= a++ + (b%(c%a)) + (a+b+c);

Test it !

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

Output printed


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|