• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Order of operations

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having a bit of a challenge to wrap my head around some logic maybe you could support me.

Rules say the following for order of operations: 1. Parentheses 2. Exponents 3. Modulo/Multiplication/Division 4. Addition/Subtraction.



The answer when running this calc equals 1 and I do not understand how it becomes 1.

Could someone explain?

//F
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 5 * 4 = 20

20 % 3 =  2

 2 - 2 =  0

 0 + 1 =  1
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Expressions in Java are evaluated from left to right.

int exp1 = 5 * 4 % 3 - 2 + 1;

1. 5 * 4 = 20
2. 20 % 3 = 2
3. 2 - 2 = 0
4. 0 + 1 = 1

Operators precedences have an effect in a way, which operands get grouped first <-- it's a bit hard to explain what I mean. Maybe someone else will have a more clear explanation about that.

 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps what's confusing you is this:

Rules say the following for order of operations: 1. Parentheses 2. Exponents 3. Modulo/Multiplication/Division 4. Addition/Subtraction.



I need to check and remind myself what those rules are..
 
Fredrik Andlin
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok based on that I thougt that I had to care of the modulo first that is where it went wrong. Thank you for clarification. //F

Stephan van Hulst wrote: 5 * 4 = 20

20 % 3 =  2

 2 - 2 =  0

 0 + 1 =  1

 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fredrik Andlin wrote:. . . order of operations: 1. Parentheses 2. Exponents 3. Modulo/Multiplication/Division 4. Addition/Subtraction. . . .

That was correct when you did your maths at school, but it isn't correct for Java®, which doesn't have an exponent operator. Not ^, which isn't an exponentiation operator at all. Joshua Bloch also says, Java® doesn't, strictly speaking, have a modulo operator; it has a remainder operator, which is slightly different. So your number 3 should read, “remainder/multiplication/division,” but that means those three operators all have the same precedence.

Maybe someone else will have a more clear explanation about that.

Yes, somebody has, and it's called the JLS (=Java®® Language Specification). It is in this section
:-

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are five factors affecting execution order:-
  • 1: The left to right rule.
  • 2: Operands are evaluated first.
  • 3: Expressions inside () are completed before the being used as an operand.
  • 4: If two adjacent operators have different precedences, that with the higher precedence is used first.
  • 5: If two adjacent operators have the same precedence, which is used first depends on its direction of association.
  • At least I think there are five factors. It's all in the JLS link I quoted a few minutes ago.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the +1s.

    A few minutes ago, I wrote:. . .

  • 5: If two adjacent operators have the same precedence, which is used first depends on its direction of association.
  • . . .

    Your expression ends with xxx − 2 + 1; those two operators have the same precedence and associate to the left. Because those arithmetic operators associate to the left, the − is used before the +.
    [I think the only right‑associative operators are the unary (prefix) operators, assignment, and ?:]. An example here:-The preincrement operator on the right (++) is executed before the negation/sign change (-), so that code prints −4.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    A few minutes ago, I wrote:. . .

  • 4: If two adjacent operators have different precedences, that with the higher precedence is used first.
  • . . .

    You had 4 % 3 - 2. That means the % is evaluated first. Had you written 4 - 3 % 2, the % would still be evaluated first.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Earlier, I wrote:. . .

  • 3: Expressions inside () are completed before the being used as an operand.
  • . . .

    Had you written 4 % (3 − 2), thewould be executed first.
    I shall go back to my last post and correct some spellling errors.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    In a previous post, I wrote:. . .

  • 2: Operands are evaluated first.
  • . . . .

    That means, in an expression like 123 + myObject.getCount(),  the method call completes before it is applied to the preceding + operator.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Not long ago, I wrote:. . .

  • 1: And a partridge in a pear tree.
  • . . .

    This is the one that really causes confusion. Look at this old thread, and the link I quoted there. If you have something like:-The ++i is executed before the i-- because it is on the left.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Different version of code from yesterday:-Again the operation on the left is executed first.
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Fredrik Andlin, cowgratulations, your topic has been published in our CodeRanch Journal.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic