• 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 qstn.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
According to precedence chart Postfix and Prefix operators have highest precedence over '/' operator.
It means Postfix and Prefix are always the first ones to execute in this expression.

int a=10;
int b=2;
float c = a++ / --b;
But this is not evaluated as according to precedence or I have got the wrong understanding.
Thanks in advance.
Shahzad
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float c = a++ / --b;
as per the rules of operator precedence, first a++ is evaluated. The value that a++ returns for taking part in the division operation is 10, and not 11, that's why it is called the postfix operator. then, --b is evaluated. This returns 1, since it is a prefix operator.
The division results in 10/1 = 10. This is converted to float since c is of type float.
So the value of c is 10.0
Hope this is helpful!
Cheeran!
Anupreet
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Muhammad Shahzad:

But this is not evaluated as according to precedence or I have got the wrong understanding.
Thanks in advance.
Shahzad


Are you expecting 11?
Two things to remember:
1. Postfix Increment/Decrement - return the orignal value in the expression then increment/decrement the variable.
2. Prefix Increment/Decrement - increment/decrement the variable then return the result in the expression.
float c = a++ / --b;
float c = 10 / 1;
float c = 10.0
If you print a after that expression you will get 11;
 
Muhammad Shahzad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your great help.
I got the concept.
Thanks again.
Shahzad
 
reply
    Bookmark Topic Watch Topic
  • New Topic