You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
22
+
[%hardbreaks]
23
+
24
+
25
+
[float]
26
+
=== Syntax
27
+
[source,arduino]
28
+
----
29
+
x %= divisor; // equivalent to the expression x = x % divisor;
30
+
----
31
+
32
+
[float]
33
+
=== Parameters
34
+
`x`: variable. *Allowed data types:* int +
35
+
`divisor`: *non zero* variable or constant. *Allowed data types:* int
36
+
37
+
--
38
+
// OVERVIEW SECTION ENDS
39
+
40
+
41
+
42
+
// HOW TO USE SECTION STARTS
43
+
[#howtouse]
44
+
--
45
+
46
+
[float]
47
+
=== Example Code
48
+
49
+
[source,arduino]
50
+
----
51
+
int x = 7;
52
+
x %= 5; // x now contains 2
53
+
----
54
+
[%hardbreaks]
55
+
56
+
[float]
57
+
=== Notes and Warnings
58
+
1. The compound remainder operator does not work on floats.
59
+
60
+
2. If the *first* operand is negative, the result is negative (or zero).
61
+
Therefore, the result of `x %= 10` will not always be between 0 and 9 if `x` can be negative.
0 commit comments