# Multiple variables in one for loop?

**URL:** <https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635>\
**Category:** Beginners\
**Created:** [June 19, 2022, 4:01am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635 "2022-06-19T04:01:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sleepycode](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@sleepycode](https://discourse.processing.org/u/sleepycode)\
**Post date:** [June 19, 2022, 4:01am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/1 "2022-06-19T04:01:34Z")

</div>

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 19, 2022, 4:05am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/2 "2022-06-19T04:05:55Z")

</div>

@sleepycode remove the second float declaration in the for loop

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

```

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 20, 2022, 2:24am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/3 "2022-06-20T02:24:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sleepycode](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@sleepycode](https://discourse.processing.org/u/sleepycode)\
**Post date:** [June 21, 2022, 3:49am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/4 "2022-06-21T03:49:40Z")

</div>

Oops yes, sorry. Just marked as solved.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 21, 2022, 8:19am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/5 "2022-06-21T08:19:46Z")

</div>

> [@Mikey83](#):
>
> ```auto
> for (float x = random(295,300), y = random(355,360); x < 700 && y < 700; x = x + 55, y = y + 55) {
>         
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 21, 2022, 8:59am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/6 "2022-06-21T08:59:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 21, 2022, 4:40pm UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/7 "2022-06-21T16:40:00Z")

</div>

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

> [@Chrisir](#):
>
> For example the for loop will probably never end.

> [@Mikey83](#):
>
> ```auto
> for (float x = random(295,300), y = random(355,360); x < 700 && y < 700; x = x + 55, y = y + 55) {
>         
> 
> ```

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

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 22, 2022, 2:24am UTC](https://discourse.processing.org/t/multiple-variables-in-one-for-loop/37635/8 "2022-06-22T02:24:04Z")

</div>

Oh, that’s right. Thanks!
