# How to use nested for-loop to draw a tree

**URL:** <https://discourse.processing.org/t/how-to-use-nested-for-loop-to-draw-a-tree/28102>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [February 26, 2021, 5:50pm UTC](https://discourse.processing.org/t/how-to-use-nested-for-loop-to-draw-a-tree/28102 "2021-02-26T17:50:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yeladogg](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yeladogg](https://discourse.processing.org/u/Yeladogg)\
**Post date:** [February 26, 2021, 5:50pm UTC](https://discourse.processing.org/t/how-to-use-nested-for-loop-to-draw-a-tree/28102/1 "2021-02-26T17:50:04Z")

</div>

Hi everyone,

Should I use nested triple for loop to do it?

This is my goal:  
 ![tree](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0d4183e9a35715eac1d836fdaa469ad2a852fe95.png)

here is my code :

```auto
int x,y,w,h;

void setup() 
{
  background(255);
  size(500, 500);

  x=250;//The center of the starting circle at the top is at position (250, 25)
  y=25;
  w=50;//width and height of ALL circles are 50
  h=50;
}

void draw() 
{
  
  fill(0,255,0);
  for(int j=0;j<=7;j++){
    for(int i=0;i<=j;i++){
   ellipse(x-25*i,y+j*50,w,h);
      for(int k=0;k<=i;k++){
 ellipse(x+25*k,y+j*50,w,h);
      }
    }
  }

}

```

---

<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 26, 2021, 6:57pm UTC](https://discourse.processing.org/t/how-to-use-nested-for-loop-to-draw-a-tree/28102/2 "2021-02-26T18:57:35Z")

</div>

A nested for-loop with 2 for-loops is enough

one for the rows and the inner for the columns (increase by 1 every new line)

try this first:

```auto
*
**
***
****
 *****

```

---

<div class="post-metadata">

**Author:** ![Yeladogg](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yeladogg](https://discourse.processing.org/u/Yeladogg)\
**Post date:** [February 27, 2021, 2:18am UTC](https://discourse.processing.org/t/how-to-use-nested-for-loop-to-draw-a-tree/28102/4 "2021-02-27T02:18:08Z")

</div>

Thank you so much, I finally did it 😄
