0
#include <iostream> using namespace std; int main() { int i = 3; i = ++i + i++; cout<<i; return 0; }
why does the above code output 9, pliz help, l was thinking it would be 7
3 Respostas
+ 5
++i of course, is pre-incremented first, so i is now 4.
So now we have
4 + i++
As i++ is a post-increment, we first add i to 4 (the original value of i), so 4 + 4, so i is now 8.
We still have that post-increment to do because of the i++, so add 1, and it's 9.
A bit of a brain twister if you don't think deep about it, but there it is :)
+ 1
l always find incrementing difficult, but eventually it becomes clear when other people explain. thanks. l guess l need too much practice.
- 1
what is this