#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Modulus

edited December 2017

I dont get really get how it works. When would I use or know when to use this also?

Tagged:

Usually you use it when you have a certain number of things you want to cycle through, and at the end of the cycle, you go back to the beginning.

``````color[] colors = { color(255,0,0), color(255,255,0), color(0,255,0), color(0,0,255), color(255,0,255) };
int current = 0;

void setup(){
size(200,200);
}

void draw(){
background( colors[current] );
}

void mousePressed(){
current += 1;
current %= colors.length;
}
``````

Here is a sketch that displays a color at a time. There are five colors it can display. When you click, you go to the next color.

What happens when you get to the color after the last color? There is no color after the last color, so it goes back to the first one.

It's also the remainder you get when you divide.

``````0 / 5 = 0
0 % 5 = 0

1 / 5 = 0
1 % 5 = 1

2 / 5 = 0
2 % 5 = 2

3 / 5 = 0
3 % 5 = 3

4 / 5 = 0
4 % 5 = 4

5 / 5 = 1
5 % 5 = 0

6 / 5 = 1
6 % 5 = 1
``````
• as the total number of seconds increases, `tick = secondCount%60` gives the current second tick, which is always a number 0-59.
• as the days roll by, `dayOfTheWeek = dayCount%7` gives the current day of the week, which is always a number between 0-6 (days 1-7).
• no matter how many 8-slice pizzas you ordered for the event, and no matter how many slices were eaten, `slice = sliceCount%8` will tell you the slice situation (0-7). If slice is 0 that means the last pizza is done and the next pizza is whole and untouched.
• no matter how many 100-sheet boxes of tissue you bought, `sheet = sheetCount%100` is how far through the current box you are. If sheet = 90 it is almost time for a new box.