I would like to know the execution flow of compound assignments in C++. I came across a CodeChef question, where I am calculating NCR mod p values and adding them together to get the final answer:
// correct
for(int i=min1; i<=max1; i+=2){
ans = (ans+ncr_mod_p(n,i))%mod;
}
// incorrect
for(int i=min1; i<=max1; i+=2){
ans+=ncr_mod_p(n,i)%mod;
}
This is happening because of integer overflow.
So, what is the execution sequence of compound assignment?
Let's say, if we have an equation a+=b%c then what would be the execution sequence:
a = (a+b)%c
// OR
a = a+(b)%c;
This statement
is equivalent to the statement
As you can see it differs from the statement
From the C++ 14 Standard (5.18 Assignment and compound assignment operators)