Skip to content

Operators for Comparison#

We've talked about operators before when we were working with numbers in variables. With the if statement, we now have a whole bunch of new operators that you can use!

Comparing Numbers#

In the previous examples, you already got to see the < and > operators - they work exactly like the ones you know from math in school. The result of this operation will be a true ✅ if something is correct or a false ❌ if it is not correct. 100 < 200 will become true ✅. But 99 > 100 will become false ❌, because clearly 99 is smaller than 100 and not greater than 100.

Comparing literal numbers is boring though, and we actually don't do this very often. What we will do a lot is comparing variables to literal numbers (like we did in the mouse examples already) or compare one variable to another variable.

float y = 0;
float yVelocity = 0;

final float FLOOR = 280;

void setup() {
    size(300, 300);
    fill(255, 255, 255);
}

void draw() {
    if (y > FLOOR) {
        y = FLOOR;
        yVelocity = random(-8, -5);
    }

    yVelocity += 0.1;
    y += yVelocity;

    background(0, 0, 0);
    circle(100, y, 10);
}

Here, we compare the ball's current position (y) to another variable that I called FLOOR. Funny how that works. Bouncy circle immediately becomes a ball in your mind. Anyway. Whenever that ball's position is greater than the arbitrary FLOOR we set earlier, then we want to set the position to be exactly the floor's height (so we can never ever dip below the floor) and then we set a new random velocity in yVelocity - starting the next jump upwards.

ALL CAPS

One of the many conventions we use as programmers is writing constants in ALL_CAPITAL_LETTERS. There's a short sentence about in "importance of good names". I also defined that one to bei final in line 4 so you really really really can't change this one, even if you wanted to.

Even more comparisons with numbers#

Besides < and >, there's also <= and >=, which are "less then or equal" and "greater than or equal" operators. They work mostly the same as their simpler counterparts, but they include the very number itself.

  • 100 < 100 is false ❌ because the left 100 is NOT smaller than the right 100.
  • 100 <= 100 is true ✅ because <= also covers the "they may be the same" possibility.

Checking for Equality of Numbers#

If you want to know if two numbers are the exact same, you can use == in Java like this:

Pseudocode
if (experiencePoints == 100) {
  experiencePoints = 0;
  level++;
}

This might be a piece of code in your standard RPG game. Once you reach a certain amount of experience, you start to level up. Please note that one line has two equals signs - this is a comparison. The other line has one equals sign - this is the boring old assignment that you already know about.

Checking for Inequality of numbers#

To check if two things are not the same, you can use the != operator. That ! means "not", so you would read this one as "A is not equal to B". A tiny example for this one:

Pseudocode
if (itemNumber != keyNumber) {
    killPlayerHorribly();
} else {
    openPodBayDoors();
}

Let's say your player gets to some door, but they chose the wrong key. This might be a trap that compares the offered item (perhaps a different key?) to the expected key. Checks like these are often done by some internal identification number. So if this number doesn't match, that means it's not the right item to open this door. The trap kills our player. End of game.

This is also the "inverse" of checking for equality (by definition, even!) - we could just as well write it like this:

Pseudocode
if (itemNumber == keyNumber) {
    openPodBayDoors();
} else {
    killPlayerHorribly();
}

Checking for Equality of Strings#

In Java, you can't compare Strings with a simple == operator. We're not going into too much detail here, but if you ever need to compare Strings, please use this form:

final String SECRET_PASSPHRASE = "Open Sesame"; 
String providedPassphrase = "Dang it open this f%\"ing door!"; // (1)

void setup() {
    size(600, 400);
}

void draw() {
    if (providedPassphrase.equals(SECRET_PASSPHRASE)) {
        background(0, 255, 0);
    } else {
        background(255, 0, 0);
    }
}
  1. Always escape your swearwords!

This is syntax we're covering much later, but I wanted you to be aware that Strings work a little differently in Java. In that example above, someone might want to enter somewhere with a passphrase of sorts. But if you come unprepared and provide the wrong passphrase, the light stays red and you can't enter.