Locked lesson.
About this lesson
How to compare two or more variables and test with logic (and, or, not).
Exercise files
Download this lesson’s related exercise files.
Comparison and Logic Operators.docx58.8 KB Comparison and Logic Operators - Solution.docx
58.8 KB
Quick reference
Comparison and Logic Operators
Comparison operators are used to compare, logic operators are used for logic.
When to use
Anytime you want to compare more than one thing, use comparison operators. Logic operators are used to do basic logic.
Instructions
The main comparison operators are:
== // equal to
!= // not equal to
> // greater than
>= // greater than or equal to
< // less than
<= // less than or equal to
The main logic operators are
&& // and
|| // or
Hints & tips
- Use comparison operators to compare things
- remember == is equal to (not =)
- 00:05 In this video, I wanna talk about comparison operators and
- 00:07 also logic operators.
- 00:09 So comparison operators allow us to compare different things.
- 00:12 Does this number equal that number?
- 00:14 Is this number greater than that number?
- 00:17 Is it less than that number?
- 00:18 Is it less than or equal to that number?
- 00:20 Very basic sort of math concept.
- 00:22 So, before we really jump into this, let's think about equal to.
- 00:25 Is this number equal to another number?
- 00:28 So we've already been using the equal to sign as an assignment operator, right?
- 00:32 So we can't use it also to compare two things.
- 00:34 You wouldn't say like, for instance, is 2 = to 4.
- 00:41 Or if you had variable x = to 2, and variable y = 3,
- 00:46 you then wouldn't say, is x = to y, 'cause all that would
- 00:52 do it would assign, it would change x from 2 to 3.
- 00:57 It would assign y to x, it would assign 3 to x.
- 01:01 So, we can't do that,
- 01:03 we need another way to use comparison operators to compare two things.
- 01:07 So very simply, we just use a double equal to sign.
- 01:10 In this case, we're asking the question, is x equal to y?
- 01:13 And in this case, the answer is no.
- 01:15 x is 2, y is 3, they're not equal.
- 01:18 If you had this is x = to y, the answer would be yes.
- 01:22 They are equal to, does equal to.
- 01:24 So that's the double equal to, that trips up a lot of people sometimes.
- 01:28 And we're gonna talk about if statements later on to where we can actually run
- 01:31 the program that will decided this, and
- 01:33 then output different things based on whether it's equal or not equal to.
- 01:36 But for now I'm just gonna sorta write it like this, so that we can see x = to y.
- 01:40 So what other comparison operators are there?
- 01:42 Well, we have our equal to, and we have likewise, not equal to,
- 01:48 we have greater than, we have less than, we have greater than or
- 01:53 equal to, and we have less than or equal to.
- 01:56 We've also got this question mark, this is called a ternary operator.
- 01:59 And we'll talk about this a little bit later on, but it is a comparison operator.
- 02:03 So, you probably learned about all these things back in grade school.
- 02:06 Math is, is 2 greater than 1?
- 02:10 Yes, it is.
- 02:11 Is 2 greater than 5?
- 02:13 No it's not, right?
- 02:14 So we're comparing two things.
- 02:16 Is 2 greater than or equal to 7?
- 02:19 No it's not.
- 02:20 Is 2 greater than or equal to 1?
- 02:23 Yes, it is.
- 02:23 Is 2 greater than or equal to 2?
- 02:26 Yes, it is, because it's equal to.
- 02:28 2 is equal to 2.
- 02:29 So, those are basic comparison operators.
- 02:31 Next, I wanna spend just a minute talking about logic operators,
- 02:34 in fact, let's just delete these guys.
- 02:37 And there are basically two logic operators that we wanna look at.
- 02:41 And that is and, and or.
- 02:44 And that's and and or.
- 02:47 Programming language is sometimes they just use the word and, or the word or.
- 02:51 JavaScript uses this double and sign and this double pipe sign.
- 02:55 So this allows us to ask multiple things.
- 02:58 It allows us to compare multiple things.
- 03:01 Is 2 greater than 1, and is 2 less than 100?
- 03:06 So we can do that using this and.
- 03:09 We can ask two question at once.
- 03:10 And matter of fact, we can keep on going, and is 3 greater than 4, no it's not.
- 03:17 Likewise, we can do, is 2 greater than 1, or 2 less than 100, this is true.
- 03:24 So when you use and, both of these have to be true in order for
- 03:29 the whole thing to be true, right?
- 03:32 If this is true and this is true.
- 03:35 If one of these is not true, then the whole thing ends up false.
- 03:38 On the other hand for or, one or the other has to be true.
- 03:42 If this is true, or this is true, then the whole thing's true.
- 03:46 If not, false.
- 03:47 If they're both false, then the whole thing is false.
- 03:50 So we'll get into this stuff more later, when we look at if-then statements and
- 03:53 stuff like that.
- 03:53 But there's lots of times when you need to sort of test
- 03:56 multiple things to compare multiple things, and you use this and,
- 04:00 and this or, these logic operators to sort of do that.
- 04:03 So that's comparison operators, that's logic operators.
- 04:05 In the next video, we'll take a look at strings, numbers and Boolean types.
Lesson notes are only available for subscribers.