• 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

operator precedence

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in operator precedence...
evaluation of

give a result of 7. It should be 9 since ++ has higher precedence that % or *.

The result of 7 also means 4*i is evaluated before i++ or i++%4?!

I am confused!
-Meher
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 Things to know:

1) result =5+i++%4*i ;
is equal to
result =5+(((i++)%4)*i) ;
because:
++ and -- goes first
than */ and %
and at last + -

if two operations are equal (like * and %) move from left to right

2)i++ returns the i-value before the increment (here 1)
but after this (left side of i++) i = 2

Summary :
i++%4 == 1 (as i++==1)
i*i ==2 as now i==2
5+2==....
Hope this helps...
 
Peter Ricke
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uppps

one of the last lines went wrong:

i*i ==2 as now i==2


should be 1*i ==2 as now i==2
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Meher,

Can you please tell the initial value of i here..
 
Meher Parveen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The initial value of i was 1.
Thanks for the detailed explanation Peter. That really helped.
-Meher
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great breakdown Peter. Another question related to this example. I, for some reason, can't grasp why 1 % <ANYINT> always comes out as 1. Of course above is an example as 1%4 gives us 1 and then we multiply that times 2 and add to 5 to get 7. But can you explain why 1 % any int returns 1 (I know this is basic but for some reason I just don't get it) thanks
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As of the 1.4 exam and later, this type of detail in precedence is NOT on the exam.

Related topics that are still on the exam include:

- short circuit
- pre and postfix precedence
- casting

Can any recent candidates remember any others?
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nico,

We know % always return the remainder after division

so for 8%5 we do 8/5 and take the remainder as our modulo (we know its 3)

or in other words we do,

5*(some-possible-integer)+rr=8 and take the rr value here as our modulo.here it comes out as 5*(1)+3 and we take rr as the modulo here rr=3


for 5%8 it becomes 5*(some-possible-integer)+rr where rr is the remainder
and that possible integer is 0, so here rr has to be 5.... ie modulo is 5

or putting it otherwise, (smallNo)%(bigNumber)==>(smallNo)

likewise there...
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer! Confusion though:


5%8 it becomes 5*(some-possible-integer)+rr where rr is the remainder and that possible integer is 0, so here rr has to be 5.... ie modulo is 5


Did you mean 5%8 becomes 8*(some...). If so I think you've answered and I get it: Since 8*1 would be, well too much because it would be 8 which is over 5, we do 8*0 which leaves remainder of 5. Hence, 5%8=5
Is this right?
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic