Example sketch: https://editor.p5js.org/atorresen/sketches/1Bjiig2V2

Boolean Logic

George Boole (1815-1864) wanted everything to be either true or false, with nothing in between. And so, he formalized Boolean logic: a way of computing with binary truth values, i.e. true or false.

The word boolean refers to a variable that can only have two possible values: true (1) or false (0).

Comparison operators

The results of comparisons – checking equality or inequality – are booleans. For example:

equality (==)

1 == 1   // evaluates to ?
1 == 2   // evaluates to ?

inequalities (<, >, <=, >=)

1 > 0   // evaluates to ?
5 < 2   // evaluates to ?
5 > 5   // evaluates to ?
5 >= 5   // evaluates to ?

Logical operators

Boolean logic is built using three basic operations that combine and act on booleans: and, or, and not.

and (&&)

true && true   // evaluates to ?
false && false   // evaluates to ?
true && false   // evaluates to ?

or (||)

true || true   // evaluates to ?
false || false   // evaluates to ?
true || false   // evaluates to ?

not (!)

!true   // evaluates to ?
!false   // evaluates to ?

These operators don't only work on literal true and false symbols. You can substitue in any expressions that evaluate to true or false, and you can combine multiple operators:

(12 > 10) && (3 > 5)   // evaluates to ?

("x" == "x") && !(0 > 1)   // evaluates to ?

Conditional Statements

Conditional statements check if a given condition is true or false to determine what code to execute. Conditional statements start with if and may also include fallback cases using else.

if

if (A) {
    // code evaluates if ?
}

else

if (A) {
    // code evaluates if ?
} else {
    // code evaluates if ?
}

else if

if (A) {
    // code evaluates if ?
} else if (B) {
    // code evaluates if ?
} else {
    // code evaluates if ?
}

State Variables

State variables are booleans that keep track of the “state” of a system, e.g., whether a condition is true or false. State variables are useful tools for referencing the same condition in multiple places. Here is an example in p5.js:

// define a global variable displayQ that will be true when the rectangle is displayed and false when the rectangle is hidden
let displayQ = false;

// update the value of displayQ when the mouse is pressed using mousePressed()
function mousePressed () {
  displayQ = !displayQ; // any time the mouse is pressed, change displayQ to its opposite
}

// refer to the value of displayQ within draw()
draw() {
  if (displayQ) {
    rect(50, 50, 100, 200);
  }
}

Note: I usually add a Q to the end of my state variable names to indicate that their value is either “true” or “false” (i.e., the answer to a true or false Question)

Practice

Create p5.js sketches that have the following behaviors:

  1. displays a rectangle only when the mouse is being held down (hint)/span>
  2. displays a rectangle only when the mouse is located on the bottom right half of the canvas
  3. displays a rectangle only after the mouse has been clicked 10 times (hint)/span>
  4. displays a black rectangle that turns red if the mouse has been clicked once, blue if the mouse has been clicked twice, and green if the mouse has been clicked three or more times
  5. displays a rectangle that toggles between being red and white any time the left side of the canvas is clicked

Bonus:

  1. create a rectangular button that logs “you clicked me N times” in the console whenever the rectangle is clicked, where N is the number of times the button has been clicked
  2. create a rectangle with a “drag-and-drop” effect
  3. write your own description (like the ones above) of a simple sketch that updates based on a conditional, then have a classmate figure out how to code it

Once you have attemped the exercises on your own, you can see sample solutions here. Note: there may be many different correct solutions! These are just examples.