• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

doubt in precedence

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

1)x=x*2+3;

here right hand side of the expression is evaluated first i.e x*2+3

here precedence comes into play because we have two operands then *(multiplication) must be evaluated i.e x*2
followed by +

its explained in different way in K&B in OPERATORS TOPIC

X=X*(2+3)HOW IT POSSIBLE WHEN ITS NOT GIVEN IN ()

can any one clarify it,
thanks in advance
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

X=X*(2+3)HOW IT POSSIBLE WHEN ITS NOT GIVEN IN ()



this can be achieved as
X *= 2+3
Both will generate the same output
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi madhu,

please specify the page in the book.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you dont alter the preferences by putting the brackets explicitly, by default the * gets the precedence over the +.

But as Al Mamun said, if you wrote as *=, then it may work in the other way.
 
madhu kancharla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for response

YOU MEAN TO SAY X*=2+3; IS AN EXCEPTIONAL CASE FOR THE PRECEDENCE BUT WHAT ABOUT IF YOU HAVE X=Y*2+3 IT MUST BE X=(Y*2)+3;OR X=Y*(2+3);?

I think precedence rules are not properly discussed in K&B
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by madhu kancharla:
YOU MEAN TO SAY X*=2+3; IS AN EXCEPTIONAL CASE FOR THE PRECEDENCE

This is not an exceptional case. All compound assignment operators work this way.

result = (left side) operator (right side)

BUT WHAT ABOUT IF YOU HAVE X=Y*2+3 IT MUST BE X=(Y*2)+3;OR X=Y*(2+3);?

Here you have the mathematical order.
 
BWA HA HA HA HA HA HA! Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic