- [Craig] In this short video, we're briefly going to go over some of
the common arithmetic and comparison operators you need to know when programming.
(uplifting piano jingle)
So before we dive in, let's just think about what the arithmetic logic unit performs.
It performs arithmetic operation, it performs logical comparison, and it also performs binary shifts,
and because of this, obviously, there's a number of things you expect it to be able to do.
You expect to be able to perform addition and subtraction,
You expect it to be able to compare if one item is greater than or less than the other,
and these type of actions performed by the ALU are supported by operators
that you can use in all common programming languages.
Let's take a look at some of them now.
So, we'll start with the common arithmetic operators.
Well, there's ones you'll be very familiar with from maths.
There's the plus symbol; for example,
x =7 + 2
so that's addition.
There's the minus or subtraction operator,
x = 7 - 2
There's the multiplication operator.
Now notice here, we use the star or asterisk symbol and not the letter X on the keyboard so
x = 7 * 2
And of course, to round off, there's the division symbol.
So, for example
x = 7 / 2
3.5
We then have exponentiation, so
x = 7 ^ 2
so that would be 49.
We then have modulus.
Now this one is one which candidates are often unaware of,
so when you're seeing it in the exam, when you write it in the exam, we'd use the word MOD,
different languages sometimes use different symbols,
so MOD performs a modulus.
It means how many whole numbers are left over
once you fit the number on the right into the number on the left as many times as you can.
So, if we've got
x = 7 MOD 2
how many whole times can we fit the number on the right, 2, into number on the left?
We can see here, I can fit it in three whole times.
What's left over is 1, so that's what modulus returns, it returns 1.
it returns what's left over.
In conjunction with modulus is what's called integer division,
and in your exam, you'll see it as the DIV command, although it's shown differently in other languages.
This is how many whole times does the number on the right fit into the number on the left?
So, as we can see from the previous example,
the number on the right, 2, fits three whole times into the number on the left,
so 7 DIV 2 is 3.
Now, that's important.
Integer division is very different from normal division.
With normal division, 7 divided by 2 is 3 and a half.
With integer division, so using whole numbers, 7 DIV 2 is 3.
There are also common comparison operators.
So, we have equals, so is one number equal to another number, is this value equal to this value?
And you'll notice in your exams and in most programming languages,
the equals operator is a double equals,
and that's because the single equals is typically used for assignment.
The opposite to that is not equals to, so is the value on the left not equal to the value on the right?
Now, there are certainly different ways of showing this in programming languages.
For your exam, not equals to is an explanation mark followed by an equals.
We then have the less than symbol, and you'll be used to this from maths,
and we have the less than or equals symbol.
On the flip-side, we have the greater than symbol
and we have the greater than or equals symbol.
Candidates also can trip up here in exams,
when what they mean to use is greater than or equals to but they accidentally forget and just use greater than,
so just be careful here that you're using exactly the correct comparison operator when you're constructing statements.
(uplifting piano jingle)