# Rectangle coding help

**URL:** <https://discourse.processing.org/t/rectangle-coding-help/24687>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 18, 2020, 9:49pm UTC](https://discourse.processing.org/t/rectangle-coding-help/24687 "2020-10-18T21:49:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rk0945](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@rk0945](https://discourse.processing.org/u/rk0945)\
**Post date:** [October 18, 2020, 9:49pm UTC](https://discourse.processing.org/t/rectangle-coding-help/24687/1 "2020-10-18T21:49:46Z")

</div>

```auto
int numRows;
int rowColumns;
int rowLength;

int startX;
int startY;

int rectWidth;
int rectHeight;

boolean isColour;

void setup () {
  size (900, 400);
  stroke (0);
  numRows = 1;
  rowColumns = 1;
  rowLength = 10;
  startX = 50;
  startY = 350;
  rectWidth = 75;
  rectHeight = 30;
  isColour = false;
}

void draw() {
  background (155);
  
  while (numRows < rowLength) {
    rect (startX, startY, rectWidth, rectHeight); 
    startX = startX + rectWidth;
    numRows = numRows + 1; //adding this line breaks the code thus no rectangle is drawn??!)
    print(numRows + ", ");
  }
  
}

```

**I want the rectangles to keep producing while numRows is less than rowLength**. For some reason, after adding the numRows + 1; (i.e. the count), no rectangle is produced. What am I doing wrong here?

Thanks!  
Before I add the `numRows = numRows + 1` command

 ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dc226a9d7886eac39550111345fb445641f086a7.png)

After I add the `numRows = numRows + 1` command, there’s **no rectangle being drawn**

---

<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:** [October 18, 2020, 10:14pm UTC](https://discourse.processing.org/t/rectangle-coding-help/24687/2 "2020-10-18T22:14:59Z")

</div>

Hello,

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

Is this a homework question or academic assignment?

You edited your last post to remove the code after it was solved:

> [@Very basic traffic light](https://discourse.processing.org/t/very-basic-traffic-light/24451):
>
> Thanks - problem solved!!!

`:)`

---

<div class="post-metadata">

**Author:** ![rk0945](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@rk0945](https://discourse.processing.org/u/rk0945)\
**Post date:** [October 18, 2020, 10:25pm UTC](https://discourse.processing.org/t/rectangle-coding-help/24687/3 "2020-10-18T22:25:54Z")

</div>

It’s a homework question. And apologies for that thread. I will not do that again.

---

<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:** [October 19, 2020, 12:12am UTC](https://discourse.processing.org/t/rectangle-coding-help/24687/4 "2020-10-19T00:12:24Z")

</div>

> [@rk0945](#):
>
> For some reason, after adding the numRows + 1; (i.e. the count), no rectangle is produced. What am I doing wrong here?

Hello,

Your program is executing exactly the way you coded it. After the the first draw() loop (which has rectangles) the next loop falls through the while loop (10 is not \< 10) and the canvas is cleared for each draw() cycle that follows.

I added a noLoop() to end of setup() so draw() only loops once and you can see output.  
I also added some println() statements.

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

First cycle (frame) of draw():

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f37f87631fab57303993016dc967b20174fd3b12.png)  
After that background() clears canvas and it is grey.

Try initializing variables at start of draw before the while loop.

`:)`

---

<div class="post-metadata">

**Author:** ![rk0945](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@rk0945](https://discourse.processing.org/u/rk0945)\
**Post date:** [October 19, 2020, 10:06am UTC](https://discourse.processing.org/t/rectangle-coding-help/24687/5 "2020-10-19T10:06:53Z")

</div>

I understand now. Many Thanks!
