Can someone explain how ++ operator works if it is used more than once in the same expression? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Can someone explain how ++ operator works if it is used more than once in the same expression?

#include <iostream> using namespace std; int main() { int i=0; i=i++ + i++; cout << i; int j=0; j = j++ + j++ + j++; cout << endl << j ; return 0; } // why do we have 1 and 3 ? // i expected to see 2 and 3 as an output https://code.sololearn.com/cV5244tCamDp/?ref=app

9th Jun 2017, 7:11 PM
wave rider
12 Answers
+ 4
i think it working that: i = 0 + 1++; then i is increment and then i = that what is on right side( 1 )
9th Jun 2017, 7:27 PM
Wojciech Bruski
Wojciech Bruski - avatar
+ 4
@Manual @Wojciech I like your explanations. int j=0; j = j++ + 4*j++ + 3*j++; I will try to make a quizz from this :) as @Wojciech suggested.
9th Jun 2017, 8:12 PM
wave rider
+ 4
I edited your code and now it shows how it work https://code.sololearn.com/cMkqIc8y1L6C/?ref=app
10th Jun 2017, 10:14 AM
Wojciech Bruski
Wojciech Bruski - avatar
+ 3
This is my take on it. in the comments of the copied code. https://code.sololearn.com/cqroPX4TXQWG/?ref=app
9th Jun 2017, 7:42 PM
Manual
Manual - avatar
+ 3
And if it was i= i++ + ++i , only then the value would be 2. Check out
9th Jun 2017, 11:48 PM
Jeevitha Pandurangaiah
Jeevitha Pandurangaiah - avatar
+ 3
@Jeevitha I think in your case k=0 r = k++ + k++ + k++ + k++ it is calculated like r=0+1+2+3;
10th Jun 2017, 12:13 AM
wave rider
+ 3
And if it was i= i++ + ++i , only then the value would be 2. Check out - you are right :) have fun with c++!
10th Jun 2017, 12:15 AM
wave rider
+ 2
I find it can be tricky. When I first use them, I had to play with the values. I have to try compiling three of them, to recreate your example.
9th Jun 2017, 7:37 PM
Manual
Manual - avatar
+ 2
If it is post increment i++, then first value will be assigned and then it will be increased by one.. Here in ur expression i=i++ + i++, First i will be assign as 0 thn increased by one..now the expression becomes i=0++ + i++ Then the + operator is for addition Now i has increased it is 1,in ur expression there is again post increament of i i.e. i++ same rule.. First assign thn increase.. So now as i=1 first assign it to the expression thn increase Now ur expression becomes i=0++ + 1++(aftr this i increased by 1) =0 + 1 =1 So finally, ans is i=1 Similarly for j=j++ + j++ + j++ Ans is: j=0++(increase by 1 becomes 1) + 1++ (increase by 1 becomes 2) + 2++(increase by one becomes 3) J=0+1+2=3 So the output is 1 and 3. It is very simple to solve these operators problem, if one simply understand two formulas i.e. In post increament or decreament like x++/x--, FIRST ASSIGN THEN INCREASE/DECREASE In pre increament/decreament like ++x/--x, FIRST INCREASE/DECREASE THEN ASSIGN. THANK YOU...
10th Jun 2017, 6:27 PM
Itishree Nath
Itishree Nath - avatar
+ 1
I added a comment on your code you can check it out
20th Jan 2019, 6:54 AM
Mufungo Geeks
Mufungo Geeks - avatar
0
When you say i++ + i++ it's 1+ 0 = 1. Your i value is 0 2nd time coz u r adding immediate i and not incremented i. In simple ignore last i U can follow my instructions for other examples too. As follows: J=j++ + j++ + j++; will be 1+2+0 = 3 K=k++ + k++ + k++ + k++; will be 1+2+3+0=6 U can check with 5 increments like this n and ans will b 10. Try out
9th Jun 2017, 11:43 PM
Jeevitha Pandurangaiah
Jeevitha Pandurangaiah - avatar
0
it will show error
10th Jun 2017, 12:59 PM
Ansh Goyal
Ansh Goyal - avatar