% (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

  • remainder
    : Variable. Erlaubte Datentypen:
    int
    ,
    float
    ,
    double
    .
  • dividend
    : Variable oder Konstante. Erlaubte Datentypen:
    int
    .
  • divisor
    : Nicht null Variable oder Konstante. Erlaubte Datentypen:
    int
    .

Beispielcode

1int x = 0;
2 x = 7 % 5; // x enthält jetzt 2
3 x = 9 % 5; // x enthält jetzt 4
4 x = 5 % 5; // x enthält jetzt 0
5 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 Variable
12 }

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.