Loading...
Logo
Processing Forum
hello there,
I 'm newbiee in processing. Actually programming, too. 
I'm trying so hard to understanding about while loop, inside of a while loop.
I know a while loop does its job until predicted condition is false, but what about second while loop ?
Here is the code i am working on it.

---------------------------------------------------
---------------------------------------------------
float x = 0 ;            
float y = 0;

void setup() {
      size (400, 400);
      background(255);
      noStroke();
}

void draw() {
      fill(0);
      while (x < width) {                   // if x smaller than width 
            while (x> width) {              // x = 0 when program start, so i assumed that processing skip that while loop.
                  y = y + 17 ;               // if x greater than width, y must be +17. 
            }
      rect(x, y, 15, 15);                   //draw 15*15 
rectangle at x,y positions
      x = x + 17;                               
            }
      }

---------------------------------------------------
---------------------------------------------------
This is my logical explaining why i write code like that. But,
the code is drawing only first line of canvas. So ...hmm...
Could someone explain me what is the logic behind a while loop inside of a while loop ?  
Thank you.

Replies(2)

I'm happy to explain, but in this case, it looks like neither of your while loops are accomplishing what you intend.  It appears like you wish for the items to be drawn in a grid.  Since the draw() function already repeats (the draw function is also a loop), you don't need additional loop functionality here.  You *do* need some if statements.

void draw()
{
  fill(0);
  rect(x, y, 15, 15);
  x = x + 17;
  if (x > width) {
    x = 0; // very important - reset back to left side
    y  = y + 17;
    if (y > height) {
      println("Grid is done");
      noLoop(); // optional
    }
  }
}

Now, if you wanted the grid to be drawn *faster*, you could use two nested while loops.  Then, instead of drawing a single rect per frame, we would draw all of them. It would look like the following.  Notice how I've moved the rectangle drawing to inside the inner while loop.  The rectangle needs to redraw each time thru.

void draw()
{
  fill(0);
  y = 0;
  while (y < height) {
    x = 0;
    while (x < width) {
     rect(x, y, 15, 15);
      x = x + 17;
    }
    y  = y + 17;
  }
}

For this code, the x and y don't have to be global variables, since I reset their values to zero every time the draw function is called.  So it's better like this (and get rid of the two global declarations).

void draw()
{
  fill(0);
  int y = 0;
  while (y < height) {
    int x = 0;
    while (x < width) {
     rect(x, y, 15, 15);
      x = x + 17;
    }
    y  = y + 17;
  }
}

Most folks use for-loops instead, because the code looks cleaner.  Functionally, the following code is identical to the above two while loops.

void draw()
{
  fill(0);
  for (int y = 0; y < height; y += 17) {
    for (int x = 0; x < width; x += 17) {
     rect(x, y, 15, 15);
    }
  }
}

Note that you can use x += 17  in place of x = x + 17
Now, I have a halo at the top of my head.
thank you ! : )