In this section, we're going to look at using arithmetic, relational, logical, and assignment operators. These are all used for comparing and evaluating data.

Transcript

In Chapter 3, we were looking at different logic statements, such as if statements, while loops, for loops, and switch cases, and all of these relied on some sort of condition to determine whether a particular section of code would run or not. We used a fairly basic condition to check if one value was equal to another, or if it was greater than or less than, and these signs or symbols that we used are called operators. Likewise, the numbers or values that have been used with operators are called operands. An operator is a symbol which tells the compiler to perform a specific logical or mathematical task. In this section, we'll be looking at arithmetic, relational, logical, and assignment operators. There is another type which are known as bitwise operators, but we'll cover those in the next section when we take a look at bitmath.

So most basic types of operators are arithmetic operators, and if you remember back to Chapter 3 again, we'll be referencing that chapter quite a lot here, we've already taken a look at arithmetic operators in the section on maths. We covered addition, subtraction, multiplication, and division, as well as some simple incrementing and decrementing, and that covers all but one of the arithmetic operators available in Standard C, with the exception of the modulus operator. Don't worry if you've never heard the term modulus before, it's just a fancy word for finding the remainder of a number, and it is represented by the percentage symbol. So let's take a quick look at how it works. So let's take this really simple sketch as an example. All I'm doing here is initializing the serial port, with a baud rate of 9600, and I'm creating an integer called remainder.

What we're doing is we're saying that remainder is going to be equal to the remainder of 11 divided by 3. So when you're using the modulus operator, the first number is the number that you were dividing, and the second number is what you were dividing by, and the remainder is going to be the result of that function. So we can see that 3 would go into 11 three times, with a remainder of 2, 3 3s are 9, and then another 2 to make it up to 11. And then we can print this result to our serial monitor. So if we upload that to the board, loading, go into the serial monitor, you'll see that it'll print out a 2, which is really cool. Likewise, if we change 11 to a 15, then 3 goes into 15 exactly 5 times, there's going to be no remainder. So if we run this, we're not going to get any result printed to our serial monitor, it's just going to be a 0. Fantastic! And that's a really easy way of how to use the modulus operator. It's really easy, and can be very handy for different maths and logic functions.

I'll add some quick reference guides in the workshop page in case you forget what does what. But that's arithmetic operators along with the previous maths section. So now let's take a look at relational operators. Relational operators are used for checking the state of two values, rather than changing a number. Again, if we throw back to Chapter 3, you'll remember that we used the double equal sign to determine if one value was equal to another or not equal to. We'll now go through and take a look at all of the relational operators and how to use them. So you can see here I've got a blank sketch, or almost blank, and in the loop I've created two variables, x and y, where x is equal to 6 and y is equal to 4. Now with relational operators, we've already covered a couple of these when we've been looking at using if statements and while loops and different forms of logical statements. And that's because in order to perform a logical statement, you usually need some form of logic operator.

And so these relational operators give us exactly that. So you might have already seen the standard if something is equal to, which is represented by the double equal sign. So in this instance, we could create an if statement using x and y. And what this is going to do is it's going to give us a positive result, or it's going to run the if statement if x is equal to y, otherwise it will bypass it. So we'll go through and give ourselves an if statement and an else statement there. And we'll print out a positive or a negative result to the serial monitor depending on the result. So we'll print a zero if it is negative or false and a one if it is positive. We'll add those. Got to remember to make those a string. All right, fantastic. So let's see how this runs. Wait for it to upload and we'll go to our serial monitor. And we can see a really quick stream of zeros because in fact x is not equal to y because six is not equal to four, which is cool.

Now, what if we change this? So another relational operator we can use is if it is not equal, which is pretty cool. Now the exclamation mark, as we've looked at previously, the exclamation mark is the symbol for not, and it's used in a few different operators, but in relational operators, you have an exclamation mark followed by an equal sign. If you can get confused with the order of these, just think about it logically. If x is not equal to, so you would put the exclamation mark for not, and then the equal sign afterwards. And this is saying if x is not equal to y, then we're going to print a one. And if it is, so if they are equal, we'll print a zero. I'll add a delay in there just to slow this down a little bit. So when we upload this now, it's going to print out a one if they are not equal, which in fact they are. Well, they are not, so we're going to get a true return. You see a one being printed because six is not equal to four. Fantastic.

Now the next one we're going to take a look at is the greater than or less than. So greater than or less than, if you remember some early high school maths, is represented by the arrow, or it's actually the greater than or less than symbol, but they look like arrows, and there's a left and a right pointing arrow, which is greater than or less than. And the rule of thumb here, in case you need a bit of a refresher, is that think about them as a crocodile's mouth. That's the way I was taught. And a greater than or less than symbol will always want the bigger side, the mouth of the crocodile, to eat the greater symbol or the larger symbol. So in this instance, we're checking if x is greater than, because it's on the side with the crocodile's mouth or the larger opening, is less than y. Crocodile wants to eat the bigger number. That's the way I think about it. So if x is greater than y, we will get a positive result. So when we go to our serial monitor, is x greater than y? Well, x is six and y is four, so we would expect to receive a one as our return, and we do.

Very, very good. Now we can flip that around to say if x is less than y, then we're going to get a zero result, because in fact, x is not less than y, it is greater than. So we're going to get a zero result for our serial monitor as expected. And then the two modifications you can do for these is if it is less than or equal to or greater than or equal to. Because in this instance, what if they were both six? And we wanted to check if they were less than or equal to, so if it's on that threshold, or greater than or equal to, so it's an inclusive relational operator, where we can say is less than or equal to, and again, follow the logic of how you would say is less than or equal to, or is greater than or equal to. And so there's some basic relational operators. They're really, really handy, especially for some of the logic statements that we've talked about. And now that you know a bit about relational operators, let's look at their conjoined twins, logical operators.

Logical operators are fairly straightforward, and often used to link multiple relational statements together. You've used two of them already, in fact, the double ampersand and the exclamation mark. So let's dive into logical operators. So as we've mentioned before, they often go with relational operators, because logical operators are used to combine different statements, say if something and something are true, or if something and something is not true. So let's say we have a statement if x is greater than y. But we also want to set another condition for our if loop to run, we have an extra condition that we want held up, we only want it to run, I'll change that back to four, if x is a particular value. So it has to be greater than and it has to be a particular value. Well, we would use the double ampersand logical operator. So what this is saying is, if x and you perform each set of relational operators first, or to make it a bit easier for yourself, which is a really good practice, following mathematical order of operations, you can put each different term in brackets, it'll still run just fine, but it makes it a little bit easier to operate and develops best coding practices.

So if x is greater than y, well, it is, yep. And if x is equal to six, which it is, so we would get a positive result from this, which is very cool. Fantastic. So let's take a look at the result. We can see we're getting a one as a result, which is a positive result here, because both of those conditions are true. Now we looked at the double ampersand, which is the and logical operator, it's check not a, it's not performing any operation. So what if we replaced it with or as a logical operator. Now in the next chapter, we're looking at bit mass and bitwise operators. So don't confuse and logical operators with and bitwise operators, they're very different things. You'll understand that once you've taken a look at the next section. But for now, when we refer to and or, or we're talking about them as logical operators. So the double pipe symbol or vertical bar is the symbol for an or logical operator. What this says is that if this is true, if one of these is one, that both can be one. But as long as one of them is one, if this is true, or this is true, then it then it will return a positive result, which gives if statement which will cause a statement to run the code.

So we can see that it would run at the moment because x is greater than y and x is equal to six, both of them are true, and that's okay. But if we change if x is equal to five, not six, then it would still run because x is greater than y, or x equals five, which isn't true, but one of them is true. So that's okay. If however, we said x is less than y, then both of those would be false. So it would bypass that section of code, and it would return a zero. So let's take a look at that one first make it greater than y. Take a look. Once it's finished uploading, take a look at the serial monitor, and we get a positive result. Fantastic, just as expected. Now I'll flip this around and make it less than so both of those statements in brackets are going to be false. So neither of them are true, which doesn't allow that if statement to run. So take a look back, we get a zero result. Very cool, just as we'd expect. Now the last logical operator we're going to look at is not.

Now we took a look at this in the code example just gone when we're looking at relational operators and seeing if something is not equal to something else. Now don't confuse that is not equal to with the not logical operator. What this guy does is it inverts the state of a logical statement. So let's say we have x is put this in brackets if x is less than y. You don't have to put those spaces there. I'm just doing it to make it a little easier to read. So if x is less than y at the moment, this is a false statement because x is not less than y. But what if we put this in brackets and put the exclamation mark out the front? Well, how this reads now is very different. It says if perform the section in brackets first, if x is less than y, which is false as zero, it's a false statement and not. So what that says is it is not true, which is a positive statement. So it's checking to see if something is not. And in this case, x is not greater than y. And because we have the exclamation mark, it inverts that logic, which allows the if statement to run.

So we'll allow this to run first with without the exclamation mark, without the not logical operator. And we'll see that we get a zero result. There we go. There's our zero. Now, if we put an exclamation mark in there, we will get a positive result because it's inverting the logic of that statement. We get a one exactly right, because that if statement is running, which is really, really cool. So that's a bit about logical operators and how you can use them. And there's a lot more power to them than just in simple if statements. But they're so handy for allowing you to compare and create extra conditions for different loops and statements. Now, so far, you've learned about all kinds of different operators and their uses, which is fine. But we're going to take a look now at assignment operators, which can be thought of as a shorter way of representing simple arithmetic and bitwise operators. For example, we first learned about arithmetic operators where we were able to create the expression I is equal to I plus 5, which again is perfectly valid. It would return I as I plus 5.

However, we can shorten this to read I plus equals 5, which does exactly the same thing. To understand this a little better, let's go back to the IDE and look at how these things work. So we'll get rid of the previous code example. Now we've just got our serial initialization. And as we were talking about, we can say I is equal to I plus 5. And that's fine. Now let's initialize i, perhaps to be I int I can equal 3. Then we'll serial print the result. Let's print the new result of I at that point. In fact, I will print the result before the operation as well so we can get some sort of reference. So we're printing I before we perform an operation on it and after we perform an operation on it. So if we go ahead and open up our serial monitor now, and every second is going to this 3, and 3 plus 5 is 8. Exactly what you would expect. But we can shorten this again, and this is exactly what you would expect. But we can shorten this again, and this is really handy if you're writing a lot of different logical operators.

So we can go plus equals 5. Now what this does is you see an arithmetic or a bitwise operator. We haven't learned about bitwise operators yet, but if you remember this in the next section, when we look at bitwise operators, you'll see this shorthand coming into play. So we can see I plus equals 5, and that is the exact equivalent. So if it was divided I equals 5, then it would say, all right, well, I is 3. So I equals 3 divided by 5. You just take whatever's on that left-hand side of the equation and put it over there, and then take that operator and put it in the middle. And just like that, you can really easily simplify things. For example, this isn't strictly necessary in Arduino, but if you're, if you want to use input and output pins on bare metal controllers, for example, the AVR microcontrollers, then you're going to need to perform, to use all of these different operators and some bitwise operators in there. And rather than having to write equal, something equals another statement, and then perform a bitwise operator on it, and then go through and add another statement.

It's so much quicker and saves a lot of time and a lot of, a lot of code as well to simply go, and it'll save you compiling time when you have a big program. Really, really handy. And then you can add your condition in. Perhaps you want to bit shift something to pin 4, perhaps, or pin B4 to adjust a port on an AVR microcontroller. Super, super handy stuff. Again, not 100% strictly necessary to using it in Arduino, but it's handy to know. And so that goes and applies for every other arithmetic operator as well. So multiply by, subtraction when we go into bitwise operators, the OR bitwise operation, or an AND, or different things. And you can use those to really, really good effect to simplify and compact your code so that it's much quicker and more lightweight, which is really, really cool. So that's a bit about operators. Call it operators part one, if you like. And in the next video, we're going to go on and look at bitwise operators, which are sort of a base on the same concept, but they're another beast altogether. So let's take a look.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.