while and for loop example

//int x = 0;

void setup() {
  fill(0);
  size(400,400);
}

void draw() {
 background(255,255,255);
 
 /*
 x = 0;
 while(x < width && mouseX > 0) {
   rect(x,0,mouseX/2,height);
   x = x + mouseX;
   println(x);
 } 
 */
 
 for (int x = 0; x < width && mouseX > 0; x = x+mouseX) {
     rect(x,0,mouseX/2,height);
 }
}

Bouncing Code – Start


int rectWidthHeight;

int rectX;
int rectY;

int direction = 10;

void setup() {
 
  println("setup");
  println("width " + width);
  println("height " + height);
  //frameRate(1);
  
  //rectMode(CENTER);
  
  rectWidthHeight = 50;
  
  rectX = mouseX;
  rectY = mouseY;
  
}

void draw() {
  background(120,120,120);

  rectWidthHeight = mouseX;
  
  rect((width/2)-(rectWidthHeight/2),(height/2)-rectWidthHeight/2,rectWidthHeight,rectWidthHeight);

  //println("draw");
  
  if (mousePressed) {
    rectX = mouseX;
    rectY = mouseY; 
  }
  
  if (rectX < 0 || rectY < 0) {
   direction = direction * -1; 
  } 
    
  if (rectX > width) {
       direction = direction * -1; 

  }

  if (rectY > height) {
    
       direction = direction * -1; 
  }
  
  rectX = rectX + direction;
  rectY = rectY + direction;
  
  rect(rectX,rectY,50,50);
  
}