Difference between x %= y*2 and x = x % y * 2. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Difference between x %= y*2 and x = x % y * 2.

https://code.sololearn.com/cwtEgwvXipnm/?ref=app This code show the slimmed down version of line 16. However if I change it to x = x % y * 2; I get a different result. Why is this?

13th Jun 2017, 7:26 AM
Arild
5 Answers
+ 1
In c++ Let's break it down x%=y*2 x%=(y*2) x=x%(y*2) now for x=x%y*2 % has higher precedence than * x=(x%y) *2 hope this helps.
13th Jun 2017, 7:39 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
0
yes, it does very much thank you. when its part of an equal sign, % is not taking part in the precedence race, but works more like an equal sign. Is this correct? and if so, is this also correct for all others like it? -=, += etc?
13th Jun 2017, 7:43 AM
Arild
0
Yes it is known as shorthand notation and they work all same. example: a+=b is same as a=a+b
13th Jun 2017, 7:46 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
0
well thats not what i meant, cause weve basically just shown in the first example that they dont work the same as their counterpart if the equations have operator precedence involved
13th Jun 2017, 7:48 AM
Arild
0
bump
13th Jun 2017, 11:44 AM
Arild