# Incrementing inside a nested loop

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.

``````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 ++;
}
``````

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;

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

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.

then you would could say

``````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);
}}
``````