Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

everbeen Zh
Greenhorn
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
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
Posts: 204
Ragu,
I feel it is 3+4+5=12
Correct me if i am wrong
Regards
neha

Valentin Crettaz
Gold Digger
Sheriff
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

farrukh mahmud
Ranch Hand
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

Jennifer Warren
Ranch Hand
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
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
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
Ragu

Marilyn de Queiroz
Sheriff
Posts: 9080
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.

Manoj Gupta
Greenhorn
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/)

farrukh mahmud
Ranch Hand
Posts: 47
thanks Guys actually i failed to got the point
Thanks any Ways
------------------
Regards
Farrukh Mahmud