+ 2

Can somebody explain in detail how you get this calculation to 4? Iā€™m a beginner thanks.

Int a = 3; Int b = 2; B = a++; Cout << ++b;

8th Jul 2018, 9:47 PM
Axel Inga
Axel Inga - avatar
3 Answers
+ 11
Axel Inga first a=3 b=2 then in the next line b=a++ here a++ follows first use then change rule (post increment operator) so b=3 then cout<<++b here it follows change then use rule (pre increment operator) so it changes b from 3 to 4.
8th Jul 2018, 10:59 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 8
Axel Inga Right šŸ‘šŸ˜Š
9th Jul 2018, 3:26 AM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 2
Mohammad, thank you for your help. What i seam to not understand is this: b=2 (we ignore this value right?) b=a++ (so here it gets the same value as ā€œaā€ which is 3. Right? Than, add one more because of the ++, and that would leave it to 4) I know im wrong there, but thats how i see it and i cant seam to see it the right way. I understand the rest of the code.
9th Jul 2018, 3:25 AM
Axel Inga
Axel Inga - avatar