Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Comparison operators are essential to creating logic in our code. In this video we'll cover the basic comparison operators you'll encounter everyday.
-
0:00
In our last video, we learned how to add, subtract, multiply, and
-
0:03
divide in objective C.
-
0:05
As you can imagine,
-
0:06
those arithmetic operators play a role in nearly any piece of software.
-
0:10
Just as essential, however, are the comparison operators.
-
0:13
Which as the name suggests, allow you to compare two values, and get a result.
-
0:18
However, unlike the numerical results you would get by doing arithmetic,
-
0:21
a comparison either returns a true or false.
-
0:25
Yes or no.
-
0:26
You're probably already family with some comparisons like greater than or less.
-
0:30
But let's spend a few minutes exploring the other common comparison operations.
-
0:38
The first four will probably be quite familiar to you from childhood math class,
-
0:42
and the last two may be new to you.
-
0:44
I'm back in my math ops project,
-
0:46
by the way, which is what we use to practice our arithmetical operations.
-
0:50
I'll just comment out the old code so it doesn't get in the way.
-
0:56
And then I'll give us some space down here, and I'll also
-
1:01
paste in the operations will be working on just do we have them for reference.
-
1:05
But I'll put those in the comment too.
-
1:07
But don't worry, we'll go through them one by one.
-
1:09
Earlier, I mentioned that comparisons don't return numerical results but
-
1:13
rather a yes or no.
-
1:15
Which means, we're gonna need a particular type of variable to handle the result.
-
1:19
And I hope that before I even finish that sentence, you were shouting bool
-
1:23
at your screen, since that's exactly what we're gonna need.
-
1:25
So, let's create a bool and we're gonna call it compareResult.
-
1:36
Now let's try some comparisons.
-
1:38
First we'll try greater than, for
-
1:43
which we can simply type compareResult = 5>3.
-
1:50
To translate that from computer to human, we're saying is five greater than three?
-
1:56
If true, set compare result to yes.
-
1:59
If not set compare result to no.
-
2:03
We can put a break point right here, Command + R, to run our code.
-
2:09
And, if we mouse over, we see compare
-
2:13
result is true, 5 is greater than 3.
-
2:18
Now let's try flipping our symbol.
-
2:21
I also actually like some space in here, so
-
2:23
I'm gonna put a space, and then that, I find that easier to read.
-
2:27
Let's run our code again, command + R.
-
2:30
We mouse over compare result false, 5 is definitely not less than 3.
-
2:36
We can take that result a step further and change our expression to a less than or
-
2:40
equal to.
-
2:41
Simply by adding an equals sign.
-
2:44
This can look a little funny.
-
2:45
But it actually makes sense.
-
2:47
You just read it less than or equal to.
-
2:50
We'll run that again.
-
2:52
Mouse over compareResult, false.
-
2:55
Five is definitely not less than or equal to three.
-
3:00
If we flip our operator.
-
3:03
To greater than or equal to three and run it again, we see we get true.
-
3:11
Five most definitely is greater than three.
-
3:14
Now just for kicks, let's change our three to a five and run it again.
-
3:22
Indeed we got true because 5 = 5.
-
3:26
Just to be sure, let's change this 5 to a 50, and we'll switch the sign.
-
3:36
False.
-
3:37
50 is neither less than nor equal to 5.
-
3:42
Okay, we got four down, and two to go.
-
3:46
Next, we have the double equal sign, which may look a little confusing at first,
-
3:50
sorta like a typo.
-
3:52
But in actuality, it's pretty simple.
-
3:54
So to do that, let's erase what we have here and we'll say 5 == 5.
-
4:00
Remember, the double equal sign is comparing two numbers, and
-
4:04
then determining if they're equal or not.
-
4:07
Remember, a single equals sign assigns a value.
-
4:10
It's called the assignment operator.
-
4:12
A double equals sign, like we have here, compares two values for equality.
-
4:17
Let's run this.
-
4:23
True, 5 does equal 5.
-
4:25
Now let's change it a bit.
-
4:33
False, 5 does not equal 5.75.
-
4:37
Okay, last up the not equal operator.
-
4:41
As you might guess, this decides if two values are not equal and if so,
-
4:45
returns yes.
-
4:46
This is written simply as an exclamation point followed by an equal sign.
-
4:52
If we run that, we'll see that we get true,
-
4:57
5 is not equal to 5.75.
-
5:02
In English, we could read this whole line as, is 5 not equal to 5.75?
-
5:08
If so, return true.
-
5:12
If not, return false.
-
5:13
In our case, it's gonna return true, because these two numbers are not equal.
-
5:18
By the way, that exclamation mark is often referred to as a bang in programming.
-
5:23
That's this exclamation mark right here.
-
5:26
Though unlike clever jargon, like camelcase or
-
5:28
breadcrumb, the slang wasn't created or popularized by programmers.
-
5:32
In actuality, it was popularized by the female secretaries of the 1950s,
-
5:37
who often used the term bang when typing in shorthand.
-
5:41
Hopefully much of what we just learned seemed familiar or
-
5:43
at least intuitive to you.
-
5:45
But if not, don't despair.
-
5:46
Using comparison operators like less than or equal to is something you'll get
-
5:50
plenty of practice with as we work through more and more code examples.
-
5:54
That said, if you feel like you need a good cheat sheet on hand,
-
5:57
I've linked one in the teacher's notes.
You need to sign up for Treehouse in order to download course files.
Sign up