Arithmetic Operators

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
2h 17m
Students
37
Ratings
5/5
starstarstarstarstar
Description

This course provides you with a solid understanding of the fundamentals of C++. We will take a look at the components of the programming language and then put these into practice through a couple of projects that we will run through at the end of the course.

Learning Objectives

  • Learn how to store different types of data in main memory
  • Understand how to manipulate and perform operations on that data, including performing arithmetic on numbers
  • Understand how programs make decisions
  • Learn how you can write your programs to communicate with users

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • Experienced C++ programmers who want to stay sharp!
  • College students and anyone studying C++

Prerequisites

This is a beginner-level course and so no prior knowledge of C++ is necessary.

 

Transcript

So, we just got done learning about how to do very basic things with variables and literals and also how to write comments. These are all fundamental constructs of a programming language like C++ that we need to understand in order to become fantastic software developers. Another major skill that you need to acquire once you know the basics of working with variables and literal values is to perform various operations on them. In C++, there are several special symbols used to indicate an operation is to be performed. We call these special symbols, operators. The data often in the form of variables or literal values that the operators are applied to are called operands. O-P-E-R-A-N-D-S. In this lecture, we will specifically look at some arithmetic or arithmetic operators that we can use to operate on our data. Let me create a new project, and I will list these arithmetic operators and some comments. Let's call the project ArithmeticFun. So, I have an empty project under C++, I hit 'Next' and I'll call it ArithmeticFun,  or  ArithmeticFun and then hit 'Create'. Once this has loaded, I'm going to of course create our source file by right clicking 'Source Files' going to 'Add', 'New item' and then I'm going to rename this main.cpp. So, let's get our skeleton code ready and we are going to make a special use of the comments we just learned. So, I'm going to put them at the very top again. But, the multi-line comment could go in multiple locations throughout the code. It's just this is a nice place to put it right at the top where we can see it and remember where it is. I'm going to list all of the arithmetic operators. We have the + symbol right here, which is called the addition operator. We have the - symbol, which is called the subtraction operator. We have the * symbol right there. That's the multiplication operator. It's just the asterisk, right? And you also have the / symbol, which is of course the division operator. And here's one you may not be familiar with. It looks like a % sign, but it's called the modulus operator. We'll get to that in just a minute. So, inside of our main function, let's create two variables a and b. int a; int b; And we're going to also create a variable called int sum. I'm going to assign a value, a will get the value 10 and b received the value 3. And then for sum, I'm going to set the sum = a + b. So, I'm applying the addition operator to the variables a and b. So, I will say this is the operator, and a and b, which are on the other side of this operator are called the operands. Again, it's O-P-E-R-A-N-D-S. So, we can also use any of the other arithmetic operators as well. Let's create difference, product, quotient, and remainder variables. So, int difference that's going to be a - b. int product, the product is the result of a multiplication problem, so that's a * b. int quotient = a / b and then int remainder = a % b.

Now, that should give you a good hint is about what the modulus operator does. We can now see the various uses of the fundamental arithmetic operators. Let's pay really close attention to modulus, which is the % sign. It will give us the remainder of a division problem. Note that we can use these operators with variables or literals. So, let's print out the values of each of these and see what we get. So, the first one, I'm actually going to put some string literals also. I'm going to say "Sum = ", now notice that's in double quotes, so the = sign doesn't really do anything. It's just aid in the output so that  the user knows what's going on. So, we also have "Difference = " and endl of course. We have "Product = " a product. And we're going to say "Quotient = " whatever the quotient is. And then of course remainder, that's going to be whatever the remainder is, good.

Now, let's run the code and see what happens. So, 'Debug', 'Start Without Debugging', and if there's no errors, it will run, which it does. And you'll notice that the sum is 13, which we expect because that's 10 + 3, a and b, difference is 7, that's 10 -3. So, that's 7. Product 30, that's 10 *3, which is right. Now, quotient and remainder seem a little bit strange though. Quotient says 3, and you might not think of 10 divided by 3 being 3, that's 9. And what's up with the remainder like? What do these mean? Well, can you guess maybe why it's giving some strange values? Like why don't we have a decimal place here? You might remember that with integers, there is no decimal place recorded. So, not only is the variable quotient and integer, the actual division right here a divided by b, the division is being performed on two integers, there's no double insight. So, that means it's taking the 10 and dividing it by 3 but using integer division, so that means it's going to truncate or remove any decimal part. Now, the remainder might take a little bit of explaining, but we can explain it along with the quotient. So, the reason we get 3 for the quotient  as I just mentioned is that it ignores the decimal part. Another way to think of it as from an integer perspective, the 3 which is our b, fits inside of the 10 three times, but it can't fit four times because that would be 12. Now, the next question you might ask is what's left over. The 3 fits into the 10, three times but that's only 9. It has a 1 left over and that's exactly what our modules tells us. So, these are some interesting results. If we were working with doubles, then we would get a decimal part. But with the integer division of course the values are truncated. So, we lose the decimal information entirely.

Now, let's briefly do a little more, and expand our arithmetic operator knowledge just a bit. For each of the fundamental arithmetic operators there exists compound assignment operators. For example, let's look at the compound addition assignment operator. So, we're going to write some code to create a variable result and we're going to set that to 10. So, I'm going to do this int result, set that to 10, and then somewhere along the line you might do something like this result += 10. Now, that looks really, really strange. But, if we print out the result, let's see if you can guess what it's doing. So, it's compound addition assignment operator. So, it's a + followed by the assignment operator. So, let's see what happens. At the bottom you see the value 20. Now, you might think, it's doubling the result well. Let's change this to 15 and run it again. So, 'Debug', 'Start Without Debugging', and this time you see it's 25. So, it takes the original 10, adds a 15 to it and then stores that back into this variable named result. And that's what all the compound addition and subtraction and multiplication and division and modulus operators do is they take whatever you put on the right, they perform the operation to it, and then store the result back on the left. So, we can write the corresponding long form here. result = result + 15.

Now, you'd never see this in math. That would actually make your math teacher pass out. So, you probably don't want to ever write something like this. If you ever wrote x = x + 1 or x = x + 15, that really, really, makes math instructors kind of anxious. So, don't do that in front of your math instructor, but remember what we're doing with the = sign is not saying that result and result was 15 or the same number. This is the assignment operator. We're taking the value of result adding 15 to it and storing it back into the result box. So, basically you're reusing this, and you're just updating the value. So, this is a shortcut for this and that goes the same for the others. So, hopefully that clears it up a bit. So it's just a shortcut, but it's technically not necessary. You could write this over and over again if you want it to, but every time you need to increase a value by 15, or if you wanted to double a value, I could say result *= 2, which would be the compound multiplication assignment operator. And any of the other operators that you want to deal with. You could always write the long form. So, we don't really need the compound operators. But they're what we call in programming syntactic sugar. They aren't really necessary, but they make our jobs a little bit sweeter. Get it? Sugar. I know. Bad joke. But that's exactly why they're named like that. We've learned a lot so far. Almost done for this lecture. Let's learn an even quicker shortcut for two special operations, adding or subtracting a one from our variables. And this is done very commonly in programming. So, they have even more special syntax that's even shorter for us. These are called the increment and decrement operators. So, if we create an integer variable myInt and set it to five; right here and then use the increment operator. Let's see what value is printed out. So, down near the bottom, we're going to put myInt. Now this would be the value before we call the increment operator. Now here's the increment operator. All you have to do is say ++. So, this is a little different. This one does not have two operands. One on the right, one on the left. In this case, we're using a form of this operator where the operand is on the left. So, it only applies to myInt. Now let's see if you can guess what it's doing. If I print out myInt again, let's see what happens. Debug, Start Without Debugging, and we'll look for the last two values here. So, it should be clear that myInt was the value five. And it's printing out a five right there. And then after you called myInt++, it is now a six. So, what ++ does is that it increases the value of its operand by one. You could also write ++myInt, and as long as it's on its own line, these are equivalent. There are some nuances about them to make them different, but that's for a future lecture. So, this is also equivalent to myInt += 1, right? Because it would be using the compound addition assignment operator to increase by one. And we would also say it's equivalent to myInt = myInt + 1; that's the long form. But since this incrementing by one thing is so common, we have this special syntax. Now the decrement operator, if I want to decrease myInt back down to a five, I can do that. And again, we have the alternative forms. It will be the same as myInt -= 1. It would also be myInt = myInt - 1. So, let's print that out and see what we get. It should go back to five and let's make sure it does. So, we do, in fact, get the five from the original value six after the increment was performed. And then we get five again because we decremented or reduced the value of myInt by one again. We will be seeing these operators a lot more later on in the course. They're used a ton with C++ control statements called loops, which we will see later in this section. For right now, I have another short challenge for you. Create a new project called ChangeIsNice and create a variable inside main named myNum and set it to 10. So, using what you've learned in this lecture, we're going to do the following. What I'm going to do is I'm actually going to write it in Notepad for you or Notepad++ whatever you prefer. I could put it in commands but I'm going to write the instructions right here. So, the project is called ChangeIsNice and I want you to declare a variable myNum, set it to 10. Then, I want you to increase myNum's original value by five using arithmetic operators. And then, I want you to double myNum's new value three times on three different lines. Then print myNum's new final value. Okay. So, I want you to pause the video and take a crack at this and then come back. Okay. Were you able to do it? That one was the most challenging challenge we've had thus far. So, if you did get an excellent work, but if you didn't, don't get discouraged. Programming and C++ isn't always easy. It will take practice but it is worth it. So, let's solve this together. So, I'm going to create a project called ChangeIsNice. So, I actually want to close this project. File, Close Solution, I create a new project call it ChangeIsNice, hit Create. And if you recall the instructions, after I create my little skeleton source file here. I like to get this ready because it's pretty much the foundation of most programs I'm going to write. So, right here, I'm going to create a variable, an integer variable called myNum, set it to 10. And then it said that we want to increase myNum's original value by five, then double myNum's new value three times, and then print myNum's final value. So, first step is to increase it by five. We could say myNum = myNum + 5, but remember there's a compound operator that we can use, which is an arithmetic operator. Then, I want to double myNum's new value three times. So, I'm going to use the shortcut compound operator as well. There's once, myNum*= 2; that will double it. Right? Because it takes the original value or whatever myNum is on line 8, and it multiplies it by two and then stores the original value of that value back in myNum. So, there we go again. We can do this three times. And then, it says to print myNum's value. So, let's run us and see what we get. 120. Let's see if that makes sense. So, we started with myNum = 10; we added five to it, so that makes it 15. Double that. By the end of this line, it'll be 30. By the end of line 9, it will be 60. And then we double it again by the end of line 10 and that makes it 120. So, when we print this out and we get 120, that's exactly what we should expect. So, I hope you got the same value as me. And if you did, that means you did this correctly. If you didn't, again, don't be discouraged. Just see what I did how you might have done it differently and then correct it. So, in this lecture, we did a ton of work. I wanted to make sure we took our time because this is a major topic, and it will affect a lot of the material in the future. Make sure you are confident with the material in this lecture before you move on. In the next lecture, we'll look at some different types of operators that help us with tasks and calculations involving Boolean values. I'll see you there.

 

About the Author
Students
316
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics