What does the % mean in the following code below

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 * asking questions

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

and % (modulo) \ Language (API) \ Processing 3+

2 Likes

Please format your code as a courtesy to the community:
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

:)

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.

What do you want to achieve?

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 –

// 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.

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

  ***if ((m % 2 == 0) ^ (n % 2 == 0)) {***
1 Like

Java operators:
Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

:)

Ah thanks. 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:

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

For example:
ArrayList \ Language (API) \ Processing 3+

States this:

For a list of the numerous ArrayList features, please read the Java reference description.

:)

1 Like