This reference is for Processing 2.0+. If you have a previous version, use the reference included with your software. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Javadoc.

Name

% (modulo)

Examples
int a = 5 % 4;            // Sets 'a' to 1
int b = 125 % 100;        // Sets 'b' to 25
float c = 285.5 % 140.0;  // Sets 'c' to 5.5 
float d = 30.0 % 33.0;    // Sets 'd' to 30.0

int a = 0;
void draw() {
  background(200);
  a = (a + 1) % width;  // 'a' increases between 0 and width 
  line(a, 0, a, height);
}
Description Calculates the remainder when one number is divided by another. For example, when 52.1 is divided by 10, the divisor (10) goes into the dividend (52.1) five times (5 * 10 == 50), and there is a remainder of 2.1 (52.1 - 50 == 2.1). Thus, 52.1 % 10 produces 2.1.

Note that when the divisor is greater than the dividend, the remainder constitutes the value of the entire dividend. That is, a number cannot be divided by any number larger than itself. For example, when 9 is divided by 10, the result is zero with a remainder of 9. Thus, 9 % 10 produces 9.

Modulo is extremely useful for keeping numbers within a boundary such as keeping a shape on the screen. (See the second example above.)
Syntax
value1 % value2
Parameters
value1 int or float
value2 int or float
Related/ (divide)
Updated on May 19, 2014 05:30:05pm PDT

Creative Commons License