This week's book giveaway is in the Jobs Discussion forum.We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Java Interview Guide this week in the Jobs Discussion forum!

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

everbeen Zh
Greenhorn

Joined: Dec 09, 2001
Posts: 2
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).]
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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).]
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Ragu,
I feel it is 3+4+5=12
Correct me if i am wrong
Regards
neha

nss
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47

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

Regards<BR>Farrukh Mahmud
Jennifer Warren
Ranch Hand

Joined: Aug 24, 2001
Posts: 53
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
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jul 20, 2001
Posts: 464
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
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9059

12
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.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
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/)

Cheers, <img src="smile.gif" border="0"> <br /><a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">Manoj</a><br />(<a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">http://www7.brinkster.com/manoj9/</a>)
farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47
thanks Guys actually i failed to got the point
Thanks any Ways
------------------
Regards
Farrukh Mahmud

I agree. Here's the link: http://aspose.com/file-tools

subject: (i++)+(i++)+(i++) ?