# Nested for loops---help please!

**URL:** https://discourse.processing.org/t/nested-for-loops-help-please/24129
**Category:** Coding Questions
**Tags:** homework
**Created:** [September 26, 2020, 12:34am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129 "2020-09-26T00:34:50Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [September 26, 2020, 12:34am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/1 "2020-09-26T00:34:50Z")

</div>

I have an assignment that states the following–Create a program using nested for statements that will produce 5 rows, 7 in each column of diagonals as shown. You will need to size the frame 450 x 450. Determine your coordinates using that scale. Use a spreadsheet program to help you decide the x and y locations to draw the diagonals. You will need to create global variables to use in the line function. The nested for statement will loop through rows and columns.

I came up with the code to use, but i need to know how i can write the following code with nested for loops?  
Also, each row of lines needs to be a different color…

size(450, 450);  
strokeWeight(8);  
fill(random(0,255), random(0,255), random(0,255));

int i=20;  
int y1=40;  
int y2=90;

for(i=20; i\<400; i +=60)  
{  
line(i, y1, i+60, y2);  
line(i, y1+60, i+60, y2+60);  
line(i, y1+120, i+60, y2+120);  
line(i, y1+180, i+60, y2+180);  
line(i, y1+240, i+60, y2+240);  
}

---

<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: [September 26, 2020, 1:00am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/2 "2020-09-26T01:00:49Z")

</div>

Hello,

I see a pattern here that could be in a loop:  
0, 60, 120, 180, 240

`:)`

---

<div class="post-metadata">

### Author: ![Hyperion65](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hyperion65/32/10569_2.png) [@Hyperion65](https://discourse.processing.org/u/Hyperion65)
#### Post date: [September 26, 2020, 1:20am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/3 "2020-09-26T01:20:42Z")

</div>

Since the universe frowns upon giving complete solutions…

you already have much of what you need. maybe it helps if you think about painting pixels in a digital image, one after another. lets say the image is 20 x 20 pixels in size.

lets say you start at the upper left pixel. you will paint one pixel after another until you reach the end.  
you can think of it as a single loop, counting from point #0 to point #399.

your “loop” would be:

```auto
i = 0 
  paint the pixel black
  add 1 to i
repeat (until i = 399)

```

but you can also say: I paint each row - **within** each row I paint each pixel

```auto
i = 0 <- thats the rows
  j = 0 <- thats the pixels in a certain row
      paint the pixel black
      add 1 to j
   repeat (until j = 19)
   add 1 to i (until i = 19)
repeat

```

that is what is missing in you approach imho  
it is a loop within a loop  
that’s what “they” mean when they say: use a **nested** loop

if you manage the line, then you will probably use that global variable for the co… 🙂

---

<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: [September 26, 2020, 3:25pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/4 "2020-09-26T15:25:28Z")

</div>

Hello,

More resources:

[6.3- For Loop - Processing Tutorial - YouTube](https://www.youtube.com/watch?v=h4ApLHe8tbk)

[6.6- Nested Loops - Processing Tutorial - YouTube](https://www.youtube.com/watch?v=H7frvcAHXps)

[https://processing.org/reference/for.html](https://processing.org/reference/for.html)

`:)`

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [September 26, 2020, 4:13pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/5 "2020-09-26T16:13:35Z")

</div>

I’ve looked at all of those and still can’t figure out how to write the code correctly

---

<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: [September 26, 2020, 4:33pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/6 "2020-09-26T16:33:25Z")

</div>

Keep working at it.

- There is a simple example in the reference I provided to plot points.
- Build on that example and draw lines (diagonals?).
- Then modify to meet the requirements of you homework assignment.
- And then try to do it from scratch without an example.

Rename variables in loops to x and y or row and col if that helps.

This is an achievable exercise.

`:)`

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/77bcfeef26f80f62b7e63cda2cc807dd5481fb3d.png)

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [September 26, 2020, 8:58pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/7 "2020-09-26T20:58:06Z")

</div>

i did that, but i am still only getting one row of diagonal lines  
int y1=40;  
int y2=90;

for(int i=20; i\<400; i = i+60)  
{  
for(int j=0; j\<400; j = j+60)

{  
line(i, y1, i+60, y2);  
}  
}

---

<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: [September 26, 2020, 9:12pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/8 "2020-09-26T21:12:49Z")

</div>

You have to use both i and j

Think of a chess board where you have rows and columns. That’s why you use a nested for loop.

---

<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: [September 26, 2020, 9:14pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/9 "2020-09-26T21:14:51Z")

</div>

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

y1 and y2 are not changing so will always be writing over the same row.

There is an example here for you to think about:  
[https://processing.org/reference/for.html](https://processing.org/reference/for.html)

println() is useful to see the values of variables in your code:  
[https://processing.org/reference/println\_.html](https://processing.org/reference/println_.html)

`:)`

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [September 27, 2020, 12:00am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/10 "2020-09-27T00:00:36Z")

</div>

where do i use j in my code?

---

<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: [September 27, 2020, 12:14am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/11 "2020-09-27T00:14:25Z")

</div>

There is an example here for you to think about and it uses i and j:  
[https://processing.org/reference/for.html](https://processing.org/reference/for.html)

`:)`

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [September 27, 2020, 12:23am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/12 "2020-09-27T00:23:17Z")

</div>

yeah, i get that…but i’m doing lines and not points…that doesn’t work for me

---

<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: [September 27, 2020, 6:01am UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/13 "2020-09-27T06:01:00Z")

</div>

Make some experiments with your code, play with it

Run it, change it, run it again

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [September 27, 2020, 5:12pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/14 "2020-09-27T17:12:07Z")

</div>

please help me figure out what to do  
i’m trying to make 5 rows of diagonal lines…  
each row has to be a different color…  
i know i need to increase y1 and y2 by 60…  
i just can’t figure out how to write it…  
i can get the first row

```auto
size(450, 450);
strokeWeight(8);
stroke(random(0,255), random(0,255), random(0,255));

int i=20;
int y1=40;
int y2=90;

for(i=20; i<400; i +=60)
{
    for(i=20; i<400; i +=60)
  {
  line(i, y1, i+60, y2);
  }
}

```

---

<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: [September 27, 2020, 5:21pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/15 "2020-09-27T17:21:14Z")

</div>

> [@glv](#):
>
> There is an example here for you to think about and it uses i and j:  
> [https://processing.org/reference/for.html](https://processing.org/reference/for.html)

> [@thughes](#):
>
> yeah, i get that…but i’m doing lines and not points…that doesn’t work for me

A point is one end of a line.

The reference page for line() can be found here along with tutorials and examples:  
[https://processing.org/](https://processing.org/)

`:)`

---

<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: [September 27, 2020, 5:37pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/16 "2020-09-27T17:37:44Z")

</div>

> [@thughes](#):
>
> `line(i, y1, i+60, y2);`

Here you calculate your line.

Here you have to use i AND j

Think of a chess board.

Play with this line, change it and run the code and see what happens

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [September 27, 2020, 5:42pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/17 "2020-09-27T17:42:53Z")

</div>

a bunch of mess! still cant figure it out

---

<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: [September 27, 2020, 8:49pm UTC](https://discourse.processing.org/t/nested-for-loops-help-please/24129/18 "2020-09-27T20:49:01Z")

</div>

solved now

thanks to all

Chrisir
