Locked lesson.
About this lesson
In this lesson, we learn about Logic and basic Comparison Operators: >, >=, <, <=, !=, ==.
Exercise files
Download this lesson’s related exercise files.
30 - Logic Comparison Operators.docx61.3 KB 30 - Logic Comparison Operators SOLUTION.docx
58.3 KB
Quick reference
Logic: Comparison Operators (>, >=, <, <=, !=, ==)
Comparison Operators allow us to compare things.
When to use
Use them whenever you need to compare things.
Instructions
The comparison operators are:
== // Equal to
!= // Not Equal To
> // Greater Than
< // Less Than
>= // Greater than or equal to
<= // Less than or equal to
Hints & tips
- These comparisons will return a True or False value
- 00:01 All right, now, we're going to start to look at logic.
- 00:06 And in this video we're going to look at comparison operators.
- 00:09 And comparison operators allow us to do just what you think they would,
- 00:12 compare two or more things.
- 00:14 So, there are a handful of comparison operators.
- 00:16 We're going to look at the main ones in this video.
- 00:18 So, let me just write out, very quickly, what these are.
- 00:20 They are, ==, !=, <, >, <=, and >=.
- 00:23 And these do just exactly what they sound like they would do.
- 00:27 They make a comparison between, at least, two things being equal to, not equal to,
- 00:31 less than, greater than, less than or equal to, or greater than or equal to.
- 00:34 So, let's just come down here and play with these a little bit,
- 00:39 just to become familiar with them.
- 00:41 So, I'm just going to create a number here.
- 00:45 So, let's say 20, and let's say == 19.
- 00:48 So, this is basically asking, is 20 == 19?
- 00:52 And you'll notice we use double equal to signs
- 00:55 here instead of just regular single equal to.
- 00:59 because you remember when we create a variable like int a,
- 01:03 and then we set it equal to something,
- 01:06 we're using this assignment operator, we're assigning 20 to a.
- 01:13 Here, we're not assigning anything, we're comparing, so we use two equal to signs.
- 01:17 That can mess people up a lot when they're just getting started.
- 01:21 You're going to forget every now and then, and
- 01:22 you're going to do something like this.
- 01:24 Now you're trying to assign 19 into 20, and that doesn't work.
- 01:28 So, comparison operators, double equal to signs.
- 01:31 Let's go ahead and save this, and run it, and see what this returns.
- 01:35 Is 20 == 19?
- 01:36 And we see, False, no, it's not.
- 01:37 And comparison operators will do just that, they will return True or False.
- 01:42 If they're true, they'll return True, if they're not, they'll return False.
- 01:46 And True or False are what is called booleans in programming languages.
- 01:51 So, we've talked about booleans are data type, True or
- 01:53 False, that's just another word for True and False.
- 01:54 So, here, we can change this around as 20 == 20, save this and run it.
- 01:59 I suspect it's going to return True, and sure enough, True, right?
- 02:04 So, just like ==, we have !=.
- 02:08 And almost always, in almost every programming language,
- 02:12 not is represented by an exclamation point like, no.
- 02:16 So, not equal to.
- 02:16 So, is 20 != 20?
- 02:19 Well, that's going to return a False, because 20 is equal to 20.
- 02:24 So it's not, not equal, not, not.
- 02:27 So, we could change this around, we could say, is 19 != 20?
- 02:33 And, of course, that's going to return True because 19 and 20 are not equal.
- 02:39 It's true that these aren't equal.
- 02:41 So, we could just run through the rest of these very quickly.
- 02:45 Is 19 < 20? Of course, this is going to return True.
- 02:51 Is 19 > 20? No, that's going to return False.
- 02:56 Sure enough, and the other ones that are a little tricky are less than or equal to or
- 03:01 greater than or equal to.
- 03:03 So this is an or situation, is it greater than or equal to?
- 03:09 So, in this case, 19 is not greater than or equal to, so, this will return False.
- 03:15 Sure enough, what about 21?
- 03:17 Is 21 >= 20?
- 03:19 Well, it's not equal to, but it is greater than.
- 03:22 And like I said, this is an or situation, either or.
- 03:25 So, this will return True.
- 03:27 If we change this to 20, 20 is not greater than 20 but it is equal to.
- 03:31 And only one of those have to be true in order for
- 03:34 the entire thing to evaluate to True.
- 03:36 So, this should evaluate to True.
- 03:39 If we hit Run, we see, sure enough, True.
- 03:41 So, those are comparison operators, super important going forward when we
- 03:46 look at logic, if else statements, which we're going to look at next.
- 03:50 Now, I'm using numbers here.
- 03:51 You can just as easily use letter strings, anything else,
- 03:54 we can compare any kind of thing you can think of.
- 03:57 We can compare strings, integers, floats, decimals, we can compare arrays,
- 04:02 objects, all kinds of things.
- 04:04 So, if we run this, John is yes, in fact, equal to John, so, we get True.
- 04:09 Here's a tricky little thing right here,
- 04:11 what do you think this is going to evaluate to?
- 04:14 Is John equal to john?
- 04:16 This is going to be False because C sharp is case sensitive.
- 04:20 So lower case j is not the same as capital J.
- 04:24 So, these two aren't equal.
- 04:26 You can't really do greater than or equal to a string.
- 04:29 So you can see we're getting red squiggly angry lines.
- 04:32 It's like, there's errors.
- 04:34 So, you can't do that obviously,
- 04:36 the operator greater than cannot be applied with operands of type string.
- 04:42 So, obviously, you can't, but you can do equal to and not equal to.
- 04:48 So, if we save this and run it,
- 04:50 this is going to return True because these two are not equal.
- 04:53 They're slightly different, lower case j, capital J, and all that good stuff.
- 04:58 So, those are the comparison operators, very important.
- 05:00 And you're likely never going to do just something like this.
- 05:03 You're going to use this in a logical statement like an if statement.
- 05:07 If John is equal to John do something else, do something else.
- 05:11 And so that's what we're going to start to look at in the next video,
- 05:14 if else statements.
- 05:15 So, that's coming right up.
Lesson notes are only available for subscribers.