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

8th Aug 2016, 8:49 PM
Michael Murandu
Michael Murandu - avatar
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 :)
8th Aug 2016, 9:08 PM
Cohen Creber
Cohen Creber - avatar
+ 1
l always find incrementing difficult, but eventually it becomes clear when other people explain. thanks. l guess l need too much practice.
8th Aug 2016, 9:18 PM
Michael Murandu
Michael Murandu - avatar
- 1
what is this
9th Aug 2016, 11:43 AM
Abo Adnan Alshik
Abo Adnan Alshik - avatar