# What does the % mean in the following code below

**URL:** <https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [July 9, 2021, 9:39pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143 "2021-07-09T21:39:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bopStu77](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@bopStu77](https://discourse.processing.org/u/bopStu77)\
**Post date:** [July 9, 2021, 9:39pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/1 "2021-07-09T21:39:38Z")

</div>

int x = 35;  
int cols, rows;

void setup() {  
size(280,280);  
strokeWeight(2);  
cols = width/x;  
rows = height/x;  
}

void draw() {  
for (int m = 0; m \< cols; m++) {  
for (int n = 0; n \< rows; n++) {

```
  ***if ((m % 2 == 0) ^ (n % 2 == 0)) {***
    fill(255);
  } else {
    fill(255,0,0);
  } 
  int x1 = m*x; 
  int y2 = n*x; 
 
  rect(x1,y2,x,x); 
} 

```

}  
}

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 9, 2021, 9:43pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/2 "2021-07-09T21:43:04Z")

</div>

% gives you the remainder after a division

`7 % 3 is 1` because the remainder is 1 (from `7 / 3 = 2`)

check the reference : [Language Reference (API) \ Processing 3+](https://www.processing.org/reference/)

and [% (modulo) \ Language (API) \ Processing 3+](https://www.processing.org/reference/modulo.html)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 9, 2021, 9:45pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/3 "2021-07-09T21:45:08Z")

</div>

Please format your code as a courtesy to the community:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

This may help:  
_[What is the Modulus Operator? A Short Guide with Practical Use Cases · Matthew J. Clemente](https://blog.mattclemente.com/2019/07/12/modulus-operator-modulo-operation.html)_

`:)`

---

<div class="post-metadata">

**Author:** ![bopStu77](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@bopStu77](https://discourse.processing.org/u/bopStu77)\
**Post date:** [July 9, 2021, 10:39pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/4 "2021-07-09T22:39:32Z")

</div>

Thank you! It was great help. But I’m wondering if there’s a alternative way to use the same code but in a different format.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 9, 2021, 10:54pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/5 "2021-07-09T22:54:56Z")

</div>

What do you want to achieve?

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [July 10, 2021, 2:05am UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/6 "2021-07-10T02:05:28Z")

</div>

> [@bopStu77](#):
>
> But I’m wondering if there’s a alternative way to use the same code but in a different format.

Yes, there is. The modulo (`%`) operators effectively track whether tiles are even or odd to fill them in alternating colours (in this case, red and white). So, you could add variables and `if`/`else` statements to keep track instead, but the code isn’t nearly as concise –

```auto
// variables to track if the column/row is odd or even
String odd_or_even_col = "even";
String odd_or_even_row = "even";

void draw() {
  for (int m = 0; m < cols; m++) {
    for (int n = 0; n < rows; n++) {
      // code for coloring even columns with alternating tiles
      if (odd_or_even_col == "even") {
        if (odd_or_even_row == "even") { fill(255, 0, 0); odd_or_even_row = "odd"; }
        else { fill(255); odd_or_even_row = "even"; }
      }
      // code for coloring odd columns with alternating tiles
      else {
        if (odd_or_even_row == "even") { fill(255); odd_or_even_row = "odd"; }
        else { fill(255, 0, 0); odd_or_even_row = "even"; }
      }
      rect(m*x, n*x, x, x);
    }
    // alternate even/odd state for each column
    if (odd_or_even_col == "even") { odd_or_even_col = "odd"; }
    else { odd_or_even_col = "even"; }
  }
}

```

Of course, there are other approaches too. For instance, you could avoid a loop-within-a-loop, opting for a single loop instead. This might make it easier to alternate colours (without using `%`), but there are other trade-offs.

---

<div class="post-metadata">

**Author:** ![pokelan](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pokelan](https://discourse.processing.org/u/pokelan)\
**Post date:** [July 10, 2021, 12:41pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/7 "2021-07-10T12:41:30Z")

</div>

Hey, another question to this code. What does actually the “^” stand for?

```auto
  ***if ((m % 2 == 0) ^ (n % 2 == 0)) {***

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 10, 2021, 12:47pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/8 "2021-07-10T12:47:29Z")

</div>

> [@pokelan](#):
>
> What does actually the “^” stand for?

> **[Caret](https://en.wikipedia.org/wiki/Caret)**
>
> Caret is the name used familiarly for the character ^ (the circumflex and a circumflex accent) provided on most QWERTY keyboards by typing .mw-parser-output .keyboard-key{border:1px solid #aaa;border-radius:0.2em;box-shadow:0.1em 0.1em 0.2em rgba(0,0,0,0.1);background-color:#f9f9f9;background-image:linear-gradient(to bottom,#eee,#f9f9f9,#eee);color:#000;padding:0.1em 0.3em;font-family:inherit;font-size:0.85em}⇧ Shift+6. The symbol has a variety of uses in programming and mathematics. The name "...

Java operators:  
_[https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html)_

`:)`

---

<div class="post-metadata">

**Author:** ![pokelan](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@pokelan](https://discourse.processing.org/u/pokelan)\
**Post date:** [July 10, 2021, 12:49pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/9 "2021-07-10T12:49:31Z")

</div>

Ah thanks. Didn’t find it in the Processing documentation.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 10, 2021, 12:55pm UTC](https://discourse.processing.org/t/what-does-the-mean-in-the-following-code-below/31143/10 "2021-07-10T12:55:41Z")

</div>

> [@pokelan](#):
>
> Didn’t find it in the Processing documentation

You are welcome!

Processing uses the Java language and not everything is listed in the Processing resources:

> **[Processing](https://en.wikipedia.org/wiki/Processing_(programming_language))**
>
> Processing is a free graphical library and integrated development environment (IDE) built for the electronic arts, new media art, and visual design communities with the purpose of teaching non-programmers the fundamentals of computer programming in a visual context.
> Processing uses the Java language, with additional simplifications such as additional classes and aliased mathematical functions and operations. It also provides a graphical user interface for simplifying the compilation and executio...

You will often see links to the Java documentation in some of the Processing references.

For example:  
_[ArrayList / Reference / Processing.org](https://processing.org/reference/ArrayList.html)_

States this:

> For a list of the numerous **ArrayList** features, please read the [Java reference description](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html).

`:)`
