• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Confused in precedence and associativity rules

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

What is the value of evaluating the following expression (- -1-3 * 10 / 5-1)?

Select the one correct answer.
a)–8
b)–6
c) 7
d) 8
e) 10
f) None of the above.


Answer given is: b

Please i need more explanation on how this expression is evaluated according to precedence and associativity rules.

Thank you.
[ August 10, 2005: Message edited by: Adil El mouden ]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't seem right. The answer should be b) -6
The best way to evaluate any mathematical expression is to try to bracket it properly

(- -1-3 * 10 / 5-1) = ((-(-1) - ((3 * 10) / 5)) - 1)

The expression on the rhs can now be evaluated by rules of arithmetic to give -6
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooh! the answer given is : b sorry
I need more explanation.
Thank you.
[ August 10, 2005: Message edited by: Adil El mouden ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code compiles as :
(-(-1)-((3*10)/5)-1)

= (1-30/5-1)
= 1-6-1
= -6

Which is the correct answer and not 7.
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to point out, that multiplication and division has higher precedence than addition and substraction

(--1) gives 0

-(3*10/5) gives -5

-5 -1 gives result -6

;-)
 
Sergei Iakhnin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to point out that

a) --1 is not a legal expression in java
b) - -1 gives 1
c) -(3*10/5) gives -6 in most cases
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Result is simple BODMAS rule

B- Bracket
O - of
D- division
M - Multipilcation
A- addition
S- Subtraction

The Expression is (- -1-3 * 10 / 5-1)

it is divided as
- of -1 -3 * 10 /5 -1
that becomes

+1 -30/5 -1

that becomes

+1 -6-1
That becomes -6

Hope this puts some more light on the ongoing discussion,

Thanks
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic