Question:
whn i=4 ; i=i++ + ++i; then i=11 the same i=4 ; j=i++ + ++i; then j=10., why the value 10 is correct if we put?
Vijay
2010-05-02 07:22:44 UTC
whn i=4 ; i=i++ + ++i; then i=11
the same i=4 ; j=i++ + ++i; then j=10., why the value 10 is correct if we put it manually.,
WHY 11 is coming ???...
Five answers:
2010-05-02 07:41:46 UTC
The expression



i++ + ++i



is evaluated from right to left. So, ++i is evaluated first, which means i == 5. then



i++ + 5



is evaluated. This equals 5 + 5 or 10, and THEN i is incremented by one, resulting in 11. The same happens when j is used, but the very last step is only performed on i, not j.



## EDIT ## Erica is right, btw. The only reason why a teacher should ask you to solve this, is to illustrate the point that one should NEVER EVER program expressions like this.
Cubbi
2010-05-02 08:48:18 UTC
You didn't specify the language, but if this is C or C++ (where such questions often arise) these expressions are ERRORS. They have no meaningful results. No correct answers.



Any compiler is allowed to do anything it wants with these. For example, on Sun C++ 5.8 2005/10/13 I am getting 10 for both i and j.



As for references:

The C Programming language, ISO 9899:1999, paragraph 6.5/2



Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.70)

70) This paragraph renders undefined statement expressions such as

i = ++i + 1;

a[i++] = i;



The C++ Programming Language, ISO 14882:2003 paragraph 5



Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined. [Example:

i = v[i++]; // the behavior is unspecified

i = 7, i++, i++; // i becomes 9

i = ++i + 1; // the behavior is unspecified

i = i + 1; // the value of i is incremented

—end example]
?
2010-05-04 08:27:12 UTC
Well very Popular n Puzzling question always asked in apti's.

i++ does post increment where as ++i does Pre Increment when related with answers

when u do i= i++ + ++i;

1)Consider any another variable say i1 = i++ & value of i = 4 then value of i will first assigned to variable "i1" & then that ++ operator does it's operation i.e. increment value by 1. So value of i initially will be i=4 & then it's value will be incremented & i1=5 & again



2) Consider any another variable say i1 = ++i but previously we have considered i1 = i++ i.e. i1=5 this value is nothing but value of "i" i.e why when we do ++i,, 5 gets incremented to 6 i.e. 5+6=11



in case of j=i++ + ++i as well when i=4 j=i++ i.e. "i"'s value is 1st assigned to j n then incremented

so j=4 n i=5 & j=++i i.e. j=4 -->+1 =5 so,5+5 =10; Here We are taking Values into consideration of L.H.S i.e. values of "i1" n "j" that's why value 10 is CORRECT !! Read Calmly,,,Hope this help!!
Shariq (http://coinsindia.info)
2010-05-02 23:14:29 UTC
Your first statement evaluated as follows



i=4;

i= 4 + 5 = 9 (It is not equals to 11 in any case like this)



In next statement i will be incremented as follows



j= 11 + 12 = 23 ( and not equals to 10)



You can check them by coding in C/C++
Erica
2010-05-02 07:37:15 UTC
This kind of expression is dangerous and should be avoided by both professional software development and school teaching. This is called side effect. Different compilers may generate different results.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...