Are you sure it's not 10 + 12 + 13 + 13, instead? That's also 48.
a++ would be 10, but increment a to 11 after using its value. Then ++a would increment a up-front, yielding 12, and use that value; ++a would increment a up-front, yielding 13, and use that value, and the final a would use the value 13.
Try leaving off the last "+ a" and see what you get. If you get 36, then my thesis is wrong and each of the a's might be 12. However, if you get 35, then the final a counts as 13, and my thesis is right.
When I do the equivalent thing in Java, which is what I have handy at the moment:
int a = 10; // Original equation
System.out.println("" + (a++ + ++a + ++a + a));
a = 10; // Omits final "a"
System.out.println("" + (a++ + ++a + ++a /*+ a*/));
a = 10; // Print each value individually
System.out.println("" + a++ + " " + ++a + " " + ++a + " " + a);
... the output is:
48
35
10 12 13 13
2007-08-03 03:20:27
·
answer #1
·
answered by McFate 7
·
1⤊
0⤋
Actually it's
10 + 12 + 13 + 13 = 48
Here is how to work this out:
If you break this down in pieces like this:
Post-increment:
result = a++
means result =a and a=a+1
Pre-increment:
result = ++a
means result =a+1 and a=a+1
Post increment adds one to variable after operation
Pre increment adds one to variable before
WORKING:
So first we have result = a++
so result = 10 and a = 11
Now add ++a
so result = 10 + (11+1) and a=11 + 1
so resut = 10 + 12 and a=12
Now add ++a
so result = 10 + 12 + (12+1) and a= 12 + 1
so result = 10 + 12 + 13 and a=13
Finally add a which is now 13
so result = 10 + 12 + 13 + 13 =48
2007-08-03 10:26:18
·
answer #2
·
answered by E.M.Bed 5
·
0⤊
0⤋
In fact, it is not 12+12+12+12 as you have just said. It is 10 + 12 +13 +13 ! At first it does not make a bigger so it is 10, later it makes bigger at once and from the action before, so it is 12, then automatically adds one, so it is 13 and then simply adds 13 again as the value of a. Here we go!
2007-08-03 11:27:17
·
answer #3
·
answered by kolibrizas 3
·
0⤊
0⤋
It's been a while since I worked with C++, but I believe the ++a argument increments before operating, while the a++ argument increments after the line is called. In your case, ++a is called twice, incrementing a to 11 and 12, respectively. After that cout statement is called, the value of a is increased to 13.
I'm sorry, but I don't know about the \v and \f escape sequences.
2007-08-03 10:23:07
·
answer #4
·
answered by Phil D 2
·
0⤊
0⤋
its correct 48 but not 12+12+12+12
lets say that a variable SUM has the summation
because its addition start from left
Step one:
before a=10, SUM=0
after a=11. SUM=10
Step two:
before a=11, SUM=10
after a=12, SUM=10+12=22
Step three:
before a=12, SUM=22
after a=13, SUM=22+13=35
Step four:
before a=13,SUM=35
after a=13,SUM=35+13=48
thats it
sorry i didnt get the second question
2007-08-03 10:24:54
·
answer #5
·
answered by Medo 3
·
0⤊
0⤋