We started looking at conditionals

  • > greater than
  • < less than
  • > = greater than or equal to
  • < = less than or equal to 
  • = = equal to
  • != not equal to

Boolean

if, else, else if

Given that x = 6 and y = 3

&&      and          (x < 10 && y > 1) is true

||         or             (x == 5 || y == 5) is false

!          not           !(x == y) is true

 


 

Try this

//some code

int x = 10;

int speed = 1;

 

void setup() {

  size(200,200);

}

 

void draw() {

  background(255);

 

  // Add the current speed to the x location.

  x = x + speed;

 

  // Remember, || means "or."

  if ((x > width) || (x < 0)) {

    // If the object reaches either edge, multiply speed by -1 to turn it around.

    speed = speed * -1;

  }

  // Display circle at x location

  stroke(0);

  fill(175);

  ellipse(x,100,32,32);

}

//  End of Code

 


Another

boolean button = false;

int x = 50;

int y = 50;

int w = 100;

int h = 75;

void setup() {

  size(480, 270); 

}

void draw() {

  // The button is pressed if (mouseX,mouseY) is inside the rectangle and mousePressed is true.

  if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h && mousePressed) {

    button = true; 

  } else {

    button = false;

  }

 if (button) {

    background(255);

    stroke(0);

  } else {

    background(0);

    stroke(255);

  }  

  fill(175);

  rect(x,y,w,h);

}

//end of snippet


While Loop

size(200, 200);

background(255);

int y = 80;       // Vertical location of each line

int x = 50;       // Initial horizontal location for first line

int spacing = 10; // How far apart is each line

int len = 20;     // Length of each line

// A variable to mark where the legs end.

int endLegs = 150; 

stroke(0);

// Draw each leg inside a while loop.

while (x <= endLegs) { 

  line (x, y, x, y + len);

  x = x + spacing;

}


//Another While loop

size(200, 200); 

background(255);

float w = width; 

while (w > 0) { 

  stroke(0); 

  fill(w); 

  ellipse(width/2, height/2, w, w); 

  w = w - 20;

}


I will add some for loops