+ 1

I don't get why the output of these are different !

Whiteout any ++ on num the output should be 4 and 7 ، with prefix ++sum it should be 5 and 9 cause it add one to the sum+3 ,but why with post fix the output become 4 and 8 it didn't add one to first one but added to second https://code.sololearn.com/c3xvm02cVoOP/?ref=ap

24th May 2019, 8:38 PM
Amir
Amir - avatar
4 odpowiedzi
+ 1
int num = 1; num += 3 // num = 4 cout << num++ ; //prints 4 but num is incremented by 1 so num = 5 num +=3 //num = 8 cout << num++ ; //prints 8 and num = 9
24th May 2019, 8:43 PM
Théophile
Théophile - avatar
+ 2
Careful, post and pre increments. num++ prints the value FIRST then adds one to it. ++num adds one to the value and THEN prints it.
24th May 2019, 8:45 PM
Choe
Choe - avatar
+ 1
++num : increments first and returns 'new' value num++ : increments after and returns 'old' value
24th May 2019, 8:44 PM
Théophile
Théophile - avatar
+ 1
Thanks ❤❤
24th May 2019, 9:50 PM
Amir
Amir - avatar