No, it is not 8.
I would guess, that on most modern compilers, the value of n will end up being 5, and i will be 4 (because postfixes are not usually evaluated until after the assignment, but prefixes and executed right away).
But, the correct answer is that both values will be undefined (that is, may vary from compiler to compiler, from program to program, and even from run to run).
According the standard, the result of expression that reads a variable more than once, and also writes it is undefined.
It is left up to the compiler and optimiser to decide the order of operations.
Some other, less crazy examples of undefined expressions in the same way are:
a[i++]=i;
or
f(a[i], i++);
etc.
2006-10-31 07:17:42
·
answer #1
·
answered by n0body 4
·
0⤊
1⤋
The value would be 8.
i++: use the value first before doing addition. So, the first i++ is 0 that is being used; but after that, i would be 1.
++i: do the addition first before using the value. So, after i++, i is now 1, do the addition would make it 2.
i++: Again, use the value first before doing the addition. Now, i is 2 and so 2 is being used. Once it is used, the value of i would be 3.
++i: Again, do the addition first before using the value. i is now 3. After the addition, i is 4 and 4 is being used.
So, the whole thing is n=0+2+2+4=8
2006-10-31 13:26:13
·
answer #2
·
answered by knitting guy 6
·
0⤊
1⤋
This answer is assuming that you are programming in C++
int i=0, n;
n = i++ + ++i + i++ + ++i;
int i=0, n;
---This line just sets the integer i equal to 0 and declares n as an integer as well
n = i++ + ++i
---If ++ precedes the variable, e.g. ++i, the value returned is the value in i after it has been incremented.
If ++ follows the variable, e.g. i++, the value returned is the value in i before it has been incremented.
so you both pre and post increment statements
so if we break that line down further we will get the answer...
i = 0 when we start
n = i++ --this means increment i after this statement so right now i =0, after this statement i = 1
so now we have n = 0 + ++i + i++ + ++i
i = 1 now
++i -- this means increment i before completing this statement, so now i = 2, after this statement i will still =2
so now we have n = 0 + 2 + i++ + ++i
at this point you just evaluate the remaining i++ and ++i as we did previously.
so next step n = 0 + 2 + 2 + ++i
this is because we increment i after the next statement, so after the i ++, i = 3
then n = 0 + 2 + 2 + 4
this is because we increment i while doing the next statement, so after ++i, i =4
so n = 0 +2 +2 +4
so n = 8
its simple to do, just confusing when you get into pre and post increments, I hope this helps. Best Wishes :-)
2006-10-31 13:16:43
·
answer #3
·
answered by rachelle105210 5
·
0⤊
1⤋
:( i'm sorry sweety I don't know.
but I really need help too and no one is answering my question. :(
http://answers.yahoo.com/question/index?qid=20061031100325AAEjcbg&r=w
[wisper] if you answer ...."I don't know" and say even "I can't help you right now" on my quesiton I'll give you points
†heaven/hellÂ¥
2006-10-31 13:19:00
·
answer #4
·
answered by Mean Puppy 3
·
0⤊
1⤋