Beginner's problem with while() loop

I just want to do a simple grid with circles, but it just doesn’t work. The circles don’t complete the grid and stop at a random point. The circles stop at a different point each time I run the program. Pls help.
I’m using 3.5.4 btw.

float x = 0;
float y = 0;
boolean a = true;

void setup()
{
  size(800, 800);
}

void draw()
{
  background(0);
  
  while(a)
  {
    ellipse(x, y, 50, 50);
    
    x= x+100;
    
    if(x > width)
    {
      y = y + 100;
      x = 0;
    }
    
    if(y > height)
    {
       y=0;
    }
    
    
    println("x", x, "y", y);
  }

   
   
}

a never gets false

what about

while(y<height) …

And get rid of this

Not working :(, the grid appears for a frame and disappears forever. But still thx!

That’s right.

Place these lines AFTER the while loop

Hey and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir

Example



float x = 0;
float y = 0;

void setup()
{
  size(800, 800);
}

void draw()
{
  background(0);

  while (y <= height)
  {
    ellipse(x, y, 50, 50);

    x = x+100;

    if (x > width)
    {
      y = y + 100;
      x = 0;
    }

    println("x", x, "y", y);
  }// closing while loop 

  // reset 
  x=0;
  y=0;
}//closing function draw() 
//
1 Like

Thx for the help!!!

1 Like

You are welcome!

Chrisir