Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic