Read-Only Mode: Your progress will not be saved, and certain functions are unavailable.Sign upto access the full features of Cratecode.

Making Things Stop

By: Cratecode

In our last (p5.js) lesson, we made our object go down to the bottom of the screen. That's pretty cool, but the object also goes right outside the screen. What if we wanted to make it stop as soon as it hit the bottom, or loop back around, or really, anything else? Let's take a look at if statements!

The basic idea behind if statements is that they let you choose if you want to run some piece of code or not. That means that we can choose to run the code that moves the object down, but only when it's not already at the bottom.

If statements look like this:


Conditions can be pretty complex, but to keep things simply, we'll start off with comparisons. With comparisons, we can compare some value to another (like checking if a variable is equal to one, for example). Here's an example of it:

// Comparisons follow the format "VALUE1 OPERATOR VALUE2". // They work a lot like math equations. if (a > 1) { console.log("BIG A"); }

Here's a little list of all the comparison operators that you can use:

  • === - equals operator to check if two things are the same.
  • !== - not equals operator to check if two things are different.
  • > - greater than operator.
  • < - less than operator.
  • >= - greater than or equal to operator.
  • <= - less than or equal to operator.

You can use all of these operators in your if statements, but right now, try making one that can check if the y position is inside of the screen, and if it is, add (remember, the y position increases when you go down, and you can find the height of the screen in the setup function).

p5.js cheatsheet

Edit this lesson

Output is unavailable in read-only mode.

AI Assistant

Making Things Stop