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
posted
0
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
posted
0
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
posted
0
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
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
posted
0
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
posted
0
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
posted
0
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: 9033
10
posted
0
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.
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
posted
0
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.