+ 1
help me
sum+=num-- sum = sum+(num-1) It is the same?
2 Réponses
+ 3
sum += num--; this statement first adds the current value of num to sum and then decrements num by 1. So, sum will contain the original value of sum plus the original value of num, and after this operation, num will be decreased by 1.
sum = sum + (num - 1); and, this statement calculates num - 1, adds the result to the current value of sum, and assigns the result back to sum. So, sum will contain the original value of sum plus the original value of num - 1, but num itself remains unchanged. This anomaly is because of the post-decrement operator -- is used in.
Check the comments in the code. You can get clearification abt the outputs. Take a look for clarification, since you didn't clearifying any Language in your tag. Here are few.
https://sololearn.com/compiler-playground/cSCk4zDIi8pR/?ref=app
https://sololearn.com/compiler-playground/c5mGR61UlStz/?ref=app
https://sololearn.com/compiler-playground/cGQB3g7Myv4T/?ref=app
+ 1
thank u very much, im understand