Question about increment operators | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Question about increment operators

I just fooled around with the Code Playground,I came across this: int x = 1; int y = ++x + ++x; cout<<y; //outputs 6? also, int x = 1; int y = ++x + x++; cout<<y; output is 5. I can't figure out how I get these results. Btw in the challenges there is a question much like this that I keep getting wrong, that's why I tried these lines in the first place.

22nd Sep 2016, 12:00 PM
Andres Mar
1 Answer
+ 2
Looks like pre-incrementation is done first, then the expression is evaluated, then post-incrementation is done, then the result of the evaluation is assigned to the variable (that last part doesn't matter here, but try doing x = x++ for example). You will only find stuff like this in exercises, so it's more of a trivia than anything else.
22nd Sep 2016, 12:16 PM
Zen
Zen - avatar