What does the% = operator do?

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

if (a > b)
   a %= b;

What is its use and commonly used?

+4
source share
5 answers

From MSDN :

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

So in your case the next line

a %= b;

matches this:

a = a % b;

Which also applies to all operators:

a += bequals a = a + b
a /= bequal a = a / b
a -= bequals a = a - b
etc.

+10
source

This is a shortcut for

a = a % b;

which gets the remainder of aand band saves the result to a.

+2
source

:

a = a % b
0

% - % #,

, 5 % 2 1.

a %= b a = a % b, ,

i+=a => i = i + a
i*=a => i = i * a
i/=a => i = i / a
0
source

%=is a compound operator. He looks likea = a % b

% used balance

5 %= 2;//1
6%=2;//0

Commonly %used to match many elements with fewer elements.

So, if you have 100 elements and you want to match them to say an array of 20 elements. You can use% operator

99%20;//19...So assign 99 to array index 19
50%20;//10...So assign 50 to array index 10
0
source

All Articles