# Can someone help me? Nested for-loop

**URL:** <https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [February 27, 2020, 12:53pm UTC](https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179 "2020-02-27T12:53:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![seojin051005](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@seojin051005](https://discourse.processing.org/u/seojin051005)\
**Post date:** [February 27, 2020, 12:53pm UTC](https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179/1 "2020-02-27T12:53:32Z")

</div>

I want to combine these two lines but I don’t know how to.

for (int x=940; x\<1001; x+=30);  
for (int y=53; y\<234; y+=30)

---

<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:** [February 27, 2020, 12:55pm UTC](https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179/2 "2020-02-27T12:55:58Z")

</div>

Hello, your mistake might be the ; after the first line

Delete this

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 27, 2020, 5:36pm UTC](https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179/3 "2020-02-27T17:36:15Z")

</div>

Chrisir is right, the semicolon ends the first statement so you have two single loops rather than a double loop. I also suggest that you use { } to delineate the code blocks like this

```auto
for (int x=940; x<1001; x+=30) {
  for (int y=53; y<234; y+=30) {
    // code to be executed in double loop
  }
}

```

Although the {} are not required if the _code to be executed_ is a single statement I suggest you use them anyway because they

- make it clear which code is included in the loops
- make it easier to add / remove / change debug code inside the loops
- reduces the risk of logic errors

---

<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:** [February 27, 2020, 5:41pm UTC](https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179/4 "2020-02-27T17:41:35Z")

</div>

typical example

```auto

void setup() {
  size(1200, 900);
}//func 

void draw() {
  for (int x=940; x<1001; x+=30) {
    for (int y=53; y<234; y+=30) {
      rect(x, y, 20, 20);
    }
  }
}//func 
//

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 27, 2020, 5:41pm UTC](https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179/5 "2020-02-27T17:41:47Z")

</div>

> [@seojin051005](#):
>
> ```auto
> for (int x=940; x<1001; x+=30);
> for (int y=53; y<234; y+=30)
> 
> ```

The problem is the `;` at the end of the 1st `for` loop, which is making it a an already complete statement block, w/o incorporating the 2nd `for` loop: 🧐

```auto
for (int x = 940; x < 1001; x += 30)
  for (int y = 53; y < 234; y += 30) {
  }

```

Another way (my fav btW): 😉

```auto
for (int x = 940; x < 1001; x += 30) for (int y = 53; y < 234; y += 30) {
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 27, 2020, 10:30pm UTC](https://discourse.processing.org/t/can-someone-help-me-nested-for-loop/18179/6 "2020-02-27T22:30:44Z")

</div>

Hello,

Welcome to the Processing Community

Some resources:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology…

[![](https://img.youtube.com/vi/h4ApLHe8tbk/maxresdefault.jpg "6.3: For Loop - Processing Tutorial") ](https://www.youtube.com/watch?v=h4ApLHe8tbk)

> **[for / Reference](https://processing.org/reference/for.html)**
>
> Controls a sequence of repetitions. A basic for structure has three parts: init, test, and update. Each part must be separated by a semicolon (;). The loop continues until …

`:)`
