Need help with this Loop

Can anyone tell me what I am doing wrong? I cannot get this to do more than one row.

float randomW;
float randomH;
float row;
float column;


void setup(){
size(400,400);
background(250,250,0);
   
for(int row = 0; row<height; row=row+20)
{
  for(int column = 0; column<width; column=column+20);
  {
randomW = random(5,10);
randomH = random(5,10);

  fill(random(0,256), random(0,256), random(0,256));
  rect(row, column, randomW, randomH);
  }}
}
2 Likes

Format your code… maybe in

rect(row, column, randomW, randomH);

you can do

rect(row * 50, column * 50, randomW, randomH);

Also, in your loops

for(int row = 0; row<height; row=row+20){
    for(int column = 0; column<width; column=column+20);{
    }
}

you could use row += 20 and column += 20

I tried your suggestions, but it didn’t change. I need it to reproduce and fill the screen with 20 rows / columns. I really thought I coded it right, but clearly I didn’t because it only produces 1 row. Thank you so much for trying to help me.

Oh I think the glaring error is the lack of a draw function… put all of your code after size(400, 400) in a

void draw(){
}

function so it refreshes every frame

Thank you for catching that. I changed it, and now you can tell that it is looping because all of the rectangles are flashing, but they are still creating only one row at the top.

float randomW;
float randomH;
float row;
float column;

void setup() {
  size(400, 400);
}

void draw() {
  background(250, 250, 0);
  for (int row = 0; row<height; row+=20) {
    for (int column = 0; column<width; column+=20) {
      randomW = random(5, 10);
      randomH = random(5, 10);
      fill(random(0, 256), random(0, 256), random(0, 256));
      rect(row, column, randomW, randomH);
    }
  }
}

You had a stray semicolon here:

 for(int column = 0; column<width; column=column+20);{

See it? Right near the end, between the ) and {?
That ruins the structure of your code, essentially making your column loop do nothing.

1 Like

That worked!!! Oh my gosh!!! Thank you so much!!!

Is there any way you could look at another problem I am having?

Cant get this to stop at 25 ellipses.

float randomX;
float randomY;
float randomZ;
float randomQ;
int x;

void setup(){
  size(400,400);
  background(250,250,0);   
}
void draw(){
randomX = random(width);
randomY = random(height);
randomZ = random(10,50);
randomQ = random(10,50);

    for (x = 0; x<25; x++)
     
  fill(random(0,256), random(0,256), random(0,256));
  ellipse(randomX, randomY, randomZ, randomQ);
}
1 Like

And where’s the curly brace to start this for loop? {?

1 Like