# Incrementing inside a nested loop

**URL:** https://discourse.processing.org/t/incrementing-inside-a-nested-loop/22034
**Category:** Coding Questions
**Created:** [June 21, 2020, 12:11pm UTC](https://discourse.processing.org/t/incrementing-inside-a-nested-loop/22034 "2020-06-21T12:11:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pessotta](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@pessotta](https://discourse.processing.org/u/pessotta)
#### Post date: [June 21, 2020, 12:11pm UTC](https://discourse.processing.org/t/incrementing-inside-a-nested-loop/22034/1 "2020-06-21T12:11:53Z")

</div>

Hello, I am trying to draw an array of rectangles in an incremented manner. I want them to increase as I click the mouse. Beginning with 2 they will increase horizontally, them move to the row below.  
I understand that the last **for** in my code is kind of doing nothing there but I left it there so maybe you can understand what I want.

I am wondering that maybe a nested loop in the wrong pathway to what I want. 😞

```auto
int rectX;
int rectY;
int numRects = 2;

void setup() {
  size(600, 600);
  background (255);
  rectMode(CENTER);
}

void draw() {
  println(numRects);
  for (rectY =50; rectY < height; rectY += 100) {
    for (rectX = 50; rectX < width; rectX += 100) {
      for (int i = 0; i < numRects; i++) {
        rect(rectX, rectY, 50, 50);
      }
    }
  }
}

void mouseClicked(){
  numRects ++;
}

```

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 21, 2020, 12:25pm UTC](https://discourse.processing.org/t/incrementing-inside-a-nested-loop/22034/2 "2020-06-21T12:25:12Z")

</div>

Can you clarify what you are trying to achieve, a double for loop will give a grid of rectangles, and you coded it correctly unless you prefer something without spaces. Is the mousepress incrementing rows or columns or both?

normally in this situation you

what might be easier is to do this;

```auto
for (rectY =0; rectY < rows; rectY ++) {
for (rectX = 0; rectX < cols; rectX ++) {
   rect(50 + rectX * 50, 50 + rectY * 50, 50, 50);
}}

```

then on mousePress you can just do rows ++ or cols ++

---

<div class="post-metadata">

### Author: ![pessotta](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@pessotta](https://discourse.processing.org/u/pessotta)
#### Post date: [June 21, 2020, 12:51pm UTC](https://discourse.processing.org/t/incrementing-inside-a-nested-loop/22034/3 "2020-06-21T12:51:45Z")

</div>

I want to increase rectangles one by one. One more to the right, then to the right, then one more to the row below on the left corner, them one more to the right, and so on.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 21, 2020, 1:02pm UTC](https://discourse.processing.org/t/incrementing-inside-a-nested-loop/22034/4 "2020-06-21T13:02:09Z")

</div>

then you would could say

```auto
for (rectY =0; rectY < rows; rectY ++) {
for (rectX = 0; rectX < cols; rectX ++) {
   int currentRect = rectx + rectY * cols;
   if(currentRect<totalRect)rect(50 + rectX * 50, 50 + rectY * 50, 50, 50);
}}

```
