02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Equality Operators

So far we've seen how to compare and see if values differ or match but we did not have a way to ask if not. In this sample we'll look into the equality operator (make it a bit more complex) and take a look at a new equality operator: the "is not equal": (!=).

In earlier videos, we came across all types of rational operators. But when we said equality operator, we just referred to (==). This is used to compare the equality of two variables and give the result. But there is another operator which is used to test the inequality of these two variables. This operator is referred as the "inequality operator" and the symbol to represent this operator is !=.

Test for Equality

Let us consider trace((a+b)==(c+5)), here a, b, c are the variables. a=5, b=6, c=-3. So the equation would result in trace(11==2), which is false. Hence, the application performs all mathematical operations before testing the equality of the equation. So == can be used to compare the equality for complicated equations as well.

Test for Inequality

This also works for inequality. We can check whether the first number is unequal to the second one or not. For example, if we have trace(a!=b), the output is true if the values of a and b are unequal and false if the values are equal. It is important to learn about this operator because we use the inequality condition in a few control statements for repetition of loops. As said earlier, detailed information about repetitive functions conditional statements will be provided later. This inequality operator can also be used to compare big and complicated equations. The application is good enough to first solve the equation on both sides and then compare the two sides to give out the Boolean result (true or false.)

Video Transcript

00:00So I've created three new variables, a, b, and c.
00:04Well, a is a number that equals 10, and b is a number that equals 20,
00:09and c is a number that equals b minus a.
00:13Or in other words, 20 minus 10, which equals 10.
00:17So c equals 10.
00:19And now in the last video, we talked about rational operators.
00:22And in this video, we're going to talk all about equality operators.
00:27So let's see what equality operators are.
00:29We already met one equality operator.
00:32And our first equality operator that we met was equals,
00:36which an equality operator, this equality operator, is a rational operator as well.
00:41And we've seen samples of, let's create a sample where it would come back as true.
00:47So it is a equals c.
00:49That's definitely true.
00:51So we don't even need to trace that because we've done that before in previous videos.
00:54So let's move on to the next item.
00:56So we've seen an equation.
00:58The next item is if we want to ask if something does not equal another item.
01:04And how do we create a not equals?
01:07Not equals is shift 1 equals exclamation point equals.
01:16Exclamation point equals is basically saying does not,
01:22or is not equal, does not equal.
01:25So if you were trying, we would trace out the same traces we've done before.
01:29A does not equal c instead of equals c.
01:32The value that we would get back would be false.
01:37And the reason is because A does equal c.
01:40So in other words, what we're asking here is does A not equal c?
01:45Is 10 not equal 10?
01:47No, 10 does is equal 10.
01:4910 is 10.
01:51Thus, if we compare 10 with 10, the value that would come back would be false.
01:56Well, if we would try to trace out, for example, let's say,
01:59is B not equal A?
02:03Is that true?
02:04Is B not equal A?
02:06Is 20 not equal 10?
02:08That is true.
02:09So that would come back as true.
02:11Okay?
02:12Let's see that in action.
02:14So we're tracing out, and we can see the first value comes back as false
02:18because A does not equal c.
02:21Well, because the equation A does not equal c is not valid
02:26because 10 is equals 10.
02:29So it's the opposite of the value that we would get if we would assign equals.
02:33Well, the second option comes back as true because B does not equal A
02:38is a valid statement.
02:4020 is not equal to 10 is a valid statement.
02:44Thus, it comes back as true.
02:46Well, the first option, which basically says 10 does not equal 10
02:51is not a valid statement.
02:54So we've seen how not equal works.
02:56And before we move away from not equal, let's make it a bit more complex
03:01because we've seen it already before.
03:03We don't really have to only trace that a bit.
03:10So we don't have to trace only direct options.
03:13Let's make it a little bit more complicated.
03:16So even before we make it in this one more complicated,
03:19let's go back to the equals and make equals a bit more complicated.
03:22How do we make it more complicated?
03:24Not because I just want to make things complicated,
03:26but let's say, for example, we wanted to check if A minus 5
03:31or let's make it to something that would actually make sense.
03:34So does A plus 10 equal B? B times 1.
03:46Let's see what we're actually doing here.
03:48So we're checking an equation between something
03:51that's even more dynamic.
03:52So A plus 10 equals 20 because A is 10.
04:00So this is 20.
04:02So this basically is 20.
04:06And then we're asking is 20 equal to B times 1, B is 20.
04:11So is 20 equal to 20, right?
04:17Does 20 equal 20?
04:21Yep, that is true.
04:23So if we run our application one more time again,
04:25we're going to see the value true because 20 does equal 20.
04:29So that is true.
04:30Let's see the same exact sample,
04:32but instead of using an equation,
04:34we're going to use a not equal.
04:37So if the value would be not equal,
04:44would this value come back as true?
04:46It would come back as false because 20 is 20.
04:49We can't say 20 is not 20.
04:51So if we trace this option out, it would come back as false.
04:55And we can see the value that comes back as false.
05:00Now let's try to use the same sample again.
05:03I'm just going to grab it and let's try to make it come back as true.
05:06How would we make it come back as true?
05:09The only way it would come back true
05:11if these two sides were not equal to each other, really.
05:14If we divide, duplicate B by, let's say, C instead,
05:18the value that would come back would be 100 on this side
05:21and 20 on this side, right?
05:23Because A plus 10 equals 20, not equals.
05:31B times C, B is 20.
05:33Oh, okay, 20 times 10 is 200, actually.
05:37Okay, is 20 equal to 200?
05:42No, 20 is not equal to 200.
05:44And we're asking if it's not equal to 200,
05:47thus it should return true.
05:50So let's delete that.
05:52So let's see if that really comes true.
05:54And it does.
05:56Very, very good.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!