Multiple variables in one for loop?

Is it possible to use two variables in a single for loop? Here’s my non-working code:

void sqrNew() {
      for (float x = random(295,300),float y = random(355,360); x < 700 && y < 700; x = x + 55, y = y + 55) {
        noStroke();
        fill(random(255),random(255),random(255));
        rect(x,y,random(40,53),random(40,53));
        println(x + " " + y);
      }
}

I keep getting syntax errors. Is there a way to make this work, or are multiple variables in a single for loop not possible?

Thanks!

@sleepycode remove the second float declaration in the for loop

void sqrNew() {
      for (float x = random(295,300), y = random(355,360); x < 700 && y < 700; x = x + 55, y = y + 55) {
        noStroke();
        fill(random(255),random(255),random(255));
        rect(x,y,random(40,53),random(40,53));
        println(x + " " + y);
      }
}
3 Likes

Hi @sleepycode can you mark it as solved if it helped, thanks

Oops yes, sorry. Just marked as solved.

2 Likes

That’s still bad code in my opinion.

For example the for loop will probably never end.

You can’t tell, how often the for loop runs.

Much better would be an i with i++, for example with an upper boundary that’s random

And x and y in separate lines

2 Likes

I agree, they wanted to know the issue they were having with the for loop, i just pointed it out

2 Likes

I agree with bad code but this imho isn’t true

as in this case x and y steadily increasing, so it will be definitely hitting the end conditions… :slight_smile:

Cheers
— mnse

2 Likes

Oh, that’s right. Thanks!