Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Let's explore a few more of these operators by starting off with a few that you already know how to use. So the most obvious ones are the arithmetic operators. We have operators for addition, subtraction, multiplication, and division which we're using the asterisk for multiplication and the forward slash for division. We're usually using this in combination with the equals sign as well. But let's say we create a couple of variables, variable a = 100, variable b, set it equal to 50, and then we can use the plus sign on the right-hand side of the assignment operator to perform this operation, to add these two together, and save the result in a new variable called result.
simply meaning, some of these operator symbols are treated as more important than others. So let's say with this simple statement, if you just read this left to right, you're going to see what's on the right -hand side of the equals sign, and you are going to say 5+5 is 10, *10 is 100. But no, the multiplication is regarded as more important. So the 5*10 is done first which is 50, and then we add 5 to it, and the result would be 55.
If I want to make sure that I can impose an order on this and I have multiple operators, I simply take the important pieces and I surround them with parentheses. So in this one for example, I can make sure that 5+5 will be evaluated by itself as 10, and then multiply it by 10 and we have 100. Now, one thing that's very common is to see the same variable name on both sides of the equals sign. But remember, what we're looking at is to evaluate whatever is on the right-hand side, we take that as an expression.
So the first thing we do here is we look at whatever the current value of score is and then we'll add 10 to it, and then we'll store the result in the variable score. So if its score is 100, it will now be 110. This idea to add a value to an existing variable rather than creating a new variable is so common that there is a shorthand for it which is +=. score += 10 just simply means take whatever the value of the score variable is and add 10 to it. Now, here the + and the = have to be written as one.
There is no space in between them. This is considered one operator, and the same way there is a +=, we also have -=, *=, and a /= for subtract a value or multiply a value or divide a value. Not only that, but it's very common to see something like this. If we want to just add 1 to an existing variable, well we could write it out such as a = a + 1 or we have that shorthand a += 1. But the idea of an increment of just adding 1 to a variable is so common, it actually has its own shorthand which is ++.
a++ simply means add 1 to the variable a. This idea is where the language C++ got its name from. It's the idea of C+1, the next version of C. And the same idea, we can subtract 1 from a variable by writing it long hand a = a - 1, or saying a -= 1 or just a--. The ++ is called the increment operator; -- is called the decrement operator.
So as you can see, these symbols have as much of an importance within a programming language as the words themselves do and we have a few more that we'll explore as the course goes on, but this will be enough to get us started.
Get unlimited access to all courses for just $25/month.Become a member