# The usage of modulo: How can I correctly implement it?

**URL:** https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494
**Category:** Coding Questions
**Created:** [January 14, 2019, 8:52am UTC](https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494 "2019-01-14T08:52:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon98404100](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon98404100](https://discourse.processing.org/u/anon98404100)
#### Post date: [January 14, 2019, 8:52am UTC](https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494/1 "2019-01-14T08:52:32Z")

</div>

In my way of learning P5.js I enountered the ‘Modulo’ function. I did some reading into it, but I fail to understand HOW I should be looking at it and using it.

I have a number ‘generator’ that generates the number 1 to 100. Our task is: For every multiplication with 5 (5, 10, 15…) a name should appear and we were asked to use modulo.

I understand that you use the ‘%’ to divide 2 numbers, but fail to understand out how I can properly use it.

Any explanation that can be given would be appreciated!

```auto
var gooi = 0;

function setup () {
    createCanvas (300, 1500); 
    noLoop();
}

function draw () {
    
    textSize(12);
    
    // i = hoogte van tekst plaatsing
        for (i = 0; i < 1000; i += 10) {

        gooi += 1;
  
        text( '(' + gooi + ')' + ' ' + 'gegooid: ' + gooi, 10, (i + 20));
        }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 14, 2019, 9:00am UTC](https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494/2 "2019-01-14T09:00:47Z")

</div>

> [@anon98404100](#):
>
> For every multiplication with 5 (5, 10, 15…) a name should appear and we were asked to use modulo.

`!(i % 5) && print(name);`

> **[% (modulo) / Reference](https://processing.org/reference/modulo.html)**
>
> 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 remain…

> **[Expressions and operators - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators)**
>
> This chapter documents all the JavaScript language operators, expressions and keywords.

---

<div class="post-metadata">

### Author: ![anon98404100](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon98404100](https://discourse.processing.org/u/anon98404100)
#### Post date: [January 14, 2019, 9:18am UTC](https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494/3 "2019-01-14T09:18:20Z")

</div>

@GoToLoop

So if I understand correctly, you use the remainder for your calculations?

like in the example of 52.1 % 10, your remainder is 2.1 and you use that?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 14, 2019, 10:11am UTC](https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494/4 "2019-01-14T10:11:40Z")

</div>

> [@anon98404100](#):
>
> Like in the example of `52.1 % 10`, your remainder is `2.1` and you use that?

AFAIK, your loop `for (i = 0; i < 1000; i += 10) {` generates only whole values for the variable _i_.

`!(i % 5) && print(name);` won’t work if _i_ is a fractional value!

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 14, 2019, 10:35am UTC](https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494/5 "2019-01-14T10:35:09Z")

</div>

a practical app for use  
modulo and floor to make a GRID  
in a one line FOR loop

```auto
// https://discourse.processing.org/t/scalable-grid-with-rectangles/7256
int x = 10, y = x, w = 20, h = w;
int grid = 24, many = grid*grid;

void setup() {
  size(500, 500);
  stroke(0, 0, 200);
  fill(0,200,0);
}

void draw() {
  background(200,200,0);
  for (int i = 0; i < many; i++) rect(x+(i%grid)*w, y+(floor(i/grid))*h, w, h); 
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [January 29, 2019, 6:22pm UTC](https://discourse.processing.org/t/the-usage-of-modulo-how-can-i-correctly-implement-it/7494/6 "2019-01-29T18:22:29Z")

</div>

A very common use of [modulo](https://processing.org/reference/modulo.html) in Processing is to perform an action every nth frame.

So, if I wanted to do something every 7 frames:

```auto
int step = 7;
void draw(){
  if(frameCount%step == 0){
    print(frameCount, " ");
  }
}

```
