Comparison operators

When we use an if statement (and certain other statements too), we usually need to make a comparison: if the number of lives equals zero, if the score is greater than the high score, if the password is the same as the one we've got stored. To do this we use comparison operators.

Assign of the times

You've probably seen this double equals by now: ==

But what does it mean, and how is it different to the single version? The single equals is often used when we set a variable:

player_score = 0

Here we're assigning the value of 0 to a variable called player_score, and so guess what? We call that = 'the assignment operator'.

So what happens if we want to compare two things to see if they're equal? Maybe we want to compare a player's lives to zero, to see whether it's game over:

player_lives = 5 if (player_lives = 0) print("Game Over!") Game Over!

What happened there? It looks like player_lives equalled 0, even though we just set it to 5. And that's exactly right, because in the if statement, with that single equals, we assigned it the value 0.

That's one way to think of it - that we set it to zero, and then tested and it was zero. Another way to think of it is that the assignment function returned true; we tested whether we could set the lives to zero, and we could, so the condition was fulfilled.

That's not as silly as it sounds - many functions return true on success and false on failure, exactly so they can be used in this way. And that also explains why we can't just say, "= is assignment, except when it's in an if statement, then it's equivalence." And that's why we need a seperate equality operator:

if (player_lives == 0) print("Game Over!")

Three's the charm

So what does === mean, and will this madness ever stop? It means "Really really equals", (more correctly called the strict equality operator), and it is needed because most languages take a loose view on equivalence. The number 0, the string '0' and the boolean false are all considered equal. But consider a scenario where we're searching through sentences looking for any that mention Fred. To do this, we might use a function like strpos(), which returns the position where one string is found within another string:

haystack = "My dog Fred has a very wet nose." needle = "Fred" position = strpos(haytack, needle); if (position) print(position) else print "Fred not found!" 7

If "Fred" isn't found, the strpos function returns false, so we get the result we expect

haystack = "My dog Grizelda has a slightly moist snozzle." needle = "Fred" [...] Fred not found!

But what about this one?

haystack = "Fred the dog wet Grizelda's nose." needle = "Fred" [...] Fred not found!

Now why is that? Because Fred was found, but right at the start - at position 0. And 0 == false. So what we need, so that we don't miss these results is the strict equality operator:

if (position === false) print "Fred not found!" else print(position)

Opportunity Nots

Often you will want to check whether two things are different - that they're NOT the same. And there's an operator for that: != There's also a strict version, the counterpart to ===, written as !==

This is often used when you need to differenciate between a result where the answer can be false, but can also legitimately be zero.

Yet another way to say "is not equal to" is to use <> - if something is greater than or less than something else, then it can't be equal to it.

Those comparison operators in full

Well, maybe not in full. But here are some that you're likely to encounter

== Equivalent
!=
<>
Not equivalent, or does not equal
=== Strictly Equivalent
!== Strictly not equivalent
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

We should probably talk about logical operators next, but maybe that's something for another day...