This is a convenient shorthand to calculate the remainder when one integer is divided by another and assign it back to the variable the calculation was done on.
x %= divisor; // equivalent to the expression x = x % divisor;
- x: variable. Allowed data types: int.
- divisor: non zero variable or constant. Allowed data types: int.
The result on Serial Monitor:
※ NOTES AND WARNINGS:
- The compound remainder operator does not work on floats.
- If the first operand is negative, the result is negative (or zero).
Therefore, the result of x %= 10 will not always be between 0 and 9 if x can be negative.
- Language : += (compound addition)
- Language : &= (compound bitwise and)
- Language : |= (compound bitwise or)
- Language : ^= (compound bitwise xor)
- Language : /= (compound division)
- Language : *= (compound multiplication)
- Language : -= (compound subtraction)
- Language : -- (decrement)
- Language : ++ (increment)
- Language : Remainder
※ ARDUINO BUY RECOMMENDATION
|Arduino UNO R3|
|Arduino Starter Kit|