Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

(i++)+(i++)+(i++) ?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
int a,i=3;
a=(i++)+(i++)+(i++);
Why a=12 in Java while 9 in C/C++ ?
Thanks.
everbeen

[This message has been edited by everbeen Zh (edited December 09, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by everbeen Zh:
Hi all,
int a,i=3;
a=(i++)+(i++)+(i++);
Why a=12 in Java while 9 in C/C++ ?
Thanks.
everbeen

[This message has been edited by everbeen Zh (edited December 09, 2001).]


In this example = has the lowest precedence and each i++ is atomic and seperate.
So it is 4 + 4 + 4 = 12
Please correct me if i am wrong
Thankx
Ragu

[This message has been edited by Ragu Sivaraman (edited December 09, 2001).]
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu,
I feel it is 3+4+5=12
Correct me if i am wrong
Regards
neha
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Neha,
int a,i=3;
a=(i++)+(i++)+(i++);
you start with i=3 which is the value used in the first i++ on the left. Then i is incremented and has the value 4 (in the middle expression). And finally on the rigth expression i has the value 5.
so 3+4+5 gives 12 and that is the answer.
Note that after that a has the value 12 and i the value 6 (since i is incremented one more time)
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi all,
int a,i=3;
a=(i++)+(i++)+(i++);
Why a=12 in Java while 9 in C/C++ ?
Thanks.
everbeen


well there is parenthesis which increases the presedence of the operator
so its 4+4+4=12
Cprrect me if i am wrong

------------------
Regards
Farrukh Mahmud
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Farrukh, if that is the case why u have the value of i as 4 rather than 3.
for me it is 3+4+5=12.
Jennifer.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Farrukh
++ has the biggest precedence, parenthesis or not !!
the expression is evaluated from left to rigth and the variable i takes successively the values 3, 4 and 5.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
Farrukh
++ has the biggest precedence, parenthesis or not !!
the expression is evaluated from left to rigth and the variable i takes successively the values 3, 4 and 5.
HIH



Thankx Val
I always have hard time in this precedence issue
I appreciate your explanations
Ragu
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For many older languages, including C and C++, the order of evaluation has been deliberately left unspecified. In other words, in C the following operands can be evaluated and added together in any order:
i + myArray[i] + functionCall();
The function may be called before, during, or after the array reference is evaluated, and the additions may be executed in any order. Some programs give different results depending on the order of evaluation.

Peter van der Linden in Just Java 2


In Java, on the other hand, the order of operand evaluation is well-defined. In fact, Java specifies not just left-to-right operand evaluation, but the order of everything else, too.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int a,i=3;
a=i+++i+++i++;
also results in 12. So, parentheses don't alter anything here.
Postfix ++ operator is evaluated as the highest precedence operator and works as
put the value and increment, put the value and increment, put the value and increment which make the expression evaluate to 3 + 4 + 5.
And so, prints 12.

------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)
 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Guys actually i failed to got the point
Thanks any Ways
------------------
Regards
Farrukh Mahmud
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic