3.2 – Logical Statements and Boolean Operators

In Processing, there is only one kind of test- true or false

  • We call this test a conditional
  • Conditionals are ways of asking if something is happening or not, and to allow our code to have different behavior in each case
  • One kind of conditional is called an IF statement
  • Instead of your code being a top-down, list of instructions, and can have different behaviors depending on what is going in your pgoram
if(something is true){
//then run this code
} //if it's not true then skip over me

This is like saying, “if this is the case, execute this code, otherwise go back to business as usual.”

But sometimes we want to say, if something is true then do this but if it’s not then I want you do something else. This is called an IF/ELSE statement.
if(something is true){
//run this code
}
else{ //otherwise
//run this code
}
if(mousePressed){
ellipse(mouseX, mouseY, 50, 50);
}
else{ //otherwise
//run this code
rect(mouseX, mouseY, 50, 50);
}
mousePressed and keyPressed
  • mousePressed and keyPressed are boolean values (this is different than void mousePressed() keyPressed() which we’ve already seen)
  • mousePressed and keyPressed are either true of false. They return true as long as the user is pressing the mouse or a key, respectively. 

IF/ELSE is like saying one or the other. But sometimes, there are multiple possibilities to a question. This is called an ELSE IF statement.

An ELISE IF statemen says “test this, if that isn’t true, then test thing other thing, if that’s false then test thing, after all these tests and everything comes up empty, then execute this last else statement.”

**You can only have one if (the first condition to test for), and one else (it’s not required, but it says all the other things weren’t true so do this instead), but you can have as many else if‘s as you like***

 

Ex.

if(mousePressed){
fill(255,0,0);
}
else if(keyPressed){
fill(0,255,0);
}
else{
fill(0,0,255);
}

You can also ask questions about numbers that you are using in your sketch.

  • Is 15 greater than 20?  –> false
  • 5 equals 5 –> true
  • 4 equals 5 –> false

The full list of questions you can ask about numbers:

  • < less than
  • > greater than
  • == equal to (**note 2 equal signs)
  • <= less than or equal
  • >= greater than or equal
  • != not equal

Now we can look at the current value stored in a variable and allow our sketch to take on different paths based on that value.

if(mouseX < width/3){
fill(255,0,0);
}
else if (mouseX < 2* width/3){
fill(0,255,0);
}
else{
fill(0,0,255);
}

We can also check to see which key has been pressed!

if(key == 'a'){
//do something } 
else if(key == 'b'){ 
//do something else 
 }

// we don't always need an else!

You can ask more advanced types of questions using “boolean operators”:

  • && and (make sure to use 2!)
  • || or
  • !  not –> “If my temperature is NOT greater than 98.6, I won’t call in sick to work.”
if (mousePressed && keyPressed) {
    ellipse(mouseX,mouseY, 10,10);
  }
  if (x > 25 || y < 100) {
    ellipse(mouseX,mouseY, 10,10);
  }
  if ( !mousePressed ) {
    ellipse(mouseX,mouseY, 10,10);
  }


Variables that incremement and decrement

  • You can reverse the polarity of a number by multiplying it by -1
Bouncing ball example:
if((xLocation > width) || (xLocation < 0)){
speed = speed * -1;
}

Leave a Reply

Your email address will not be published. Required fields are marked *