% (remainder)
Beschreibung
Die Operation Modulo berechnet den Rest, wenn eine ganze Zahl durch eine andere geteilt wird. Dies ist nützlich, um eine Variable innerhalb eines bestimmten Bereichs zu halten (z. B. die Größe eines Arrays). Das
%
(Prozent) -Symbol wird zur Ausführung der Modulo-Operation verwendet.Syntax
remainder = dividend % divisor
Parameter
: Variable. Erlaubte Datentypen:remainder
,int
,float
.double
: Variable oder Konstante. Erlaubte Datentypen:dividend
.int
: Nicht null Variable oder Konstante. Erlaubte Datentypen:divisor
.int
Beispielcode
1int x = 0;2 x = 7 % 5; // x enthält jetzt 23 x = 9 % 5; // x enthält jetzt 44 x = 5 % 5; // x enthält jetzt 05 x = 4 % 5; // x enthält jetzt 4
1/* Aktualisieren eines Werts in einem Array durch eine Schleife */2
3 int values[10];4 int i = 0;5
6 void setup() {}7
8 void loop()9 {10 values[i] = analogRead(0);11 i = (i + 1) % 10; // Modulo-Operator überschreibt Variable12 }
Anmerkungen und Warnungen
Der Modulo-Operator arbeitet nicht mit
float
-Werten.Siehe auch
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.