Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the %= operator do? [duplicate]

Tags:

c#

What does the %= operator do, as shown in this example:

if (a > b)
   a %= b;

What are its uses and is it commonly used?

like image 777
Edward Karak Avatar asked Sep 29 '13 13:09

Edward Karak


2 Answers

From MSDN:

The % operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators.

So in your case, the following string

a %= b;

is the same as this one:

a = a % b;

Which also applies to all operators:

a += b equals to a = a + b
a /= b equals to a = a / b
a -= b equals to a = a - b
etc.

like image 87
AgentFire Avatar answered Oct 21 '22 16:10

AgentFire


It's a shortcut for

a = a % b;

which gets the remainder of a and b and stores the result in a.

like image 23
John Woo Avatar answered Oct 21 '22 15:10

John Woo