+ 1
"Please, explain yourself!" ++i + i++
Please, explain the feature of: int i = 1; cout << ++i + i++; /* output: 5 */ Why?
8 Answers
+ 6
Here is a clear example:
x = 5;
y = ++x;
This means to increment x first then assign its value to y.
In this case, y will be equal to 6 and x will be equal to 6.
x = 5;
y = x++;
This means to assign the value of x to y and then increment x.
In this case, y will be equal to 5 and x will be equal to 6.
+ 3
I think that question is the most answered question in the last weeks.
++i is prefix notation and means i = i+1;
i++ is postfix notation and means i= i + 1;
The difference is how the statements get executed.
Prefix means that you execute it directly and use your incremented value.
Postfix means you use the value before incrementation and increment it afterwards.
+ 1
The post increment runs caus its in a calculation. cout has to wait till the value to print is calculated and due to the postfix notation, the calculation is in this case finoshed when I got incremented
0
AKCodingBlog, according to your explanation it has to be: (1+1) + 2 = 4
0
Pavel, you forgot to increment in the last part.
we divide your expression:
int i = 1;
++i // this increments i to 2
+
++i // this adds 2 on i so i is 4
; // after your calculation you increment i again, because of postfix notation. so i is 5;
// <--- now you output i
0
Okay, I'll try to reconstruct my question:
why postfix increment is triggering before "cout"?
It's look like posrfix don't increment it afterwards.
0
Thanks to everyone.
0
E



