How to fix my finite loop?

edited October 2016 in Questions about Code

I want it to stop at 100 dots.

float a;
float b;
int dotNumber = 0;
void setup() {
  size(200, 200);
  background(255);
}
void draw() {
  strokeWeight(3);
  a = random(200);
  b = random(200);
  dotNumber = 0;
  while(dotNumber < 100) {
    point(a, b);
    dotNumber = dotNumber + 1;
    print(dotNumber);
  }
}

Answers

  • Answer ✓

    Format your code. Edit post, select code and hit ctrl+o.

    Regarding your question. Draw is executed about 30 fps. In your code, you are calling a for loop that creates 100 points 60 times per second. I modified your code to do what you want:

    float a;
    float b;
    int dotNumber = 0;
    
    void setup() {
      size(200, 200);
      background(250);
    }
    
    void draw() {
    
      strokeWeight(3);
      a = random(200);
      b = random(200);
    
      if ( dotNumber++ < 100) {
        point(a, b);
      }
    }
    

    Think of draw() as a while loop. I hope this helps.

    Kf

  • Thanks alot!!!

Sign In or Register to comment.