Basic Arithmetic
[also applicable to C]
C++, just like any other language, has a set of arithmetic operators for you to use in your
programs. Although basic, it's nice to become familiar with their implementations.
You basic operators consist of:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
These operators work much in the way you think they would. Take for example this sample program:
#include "stdafx.h" //this is for .NET, it is the basic equivalent of #include <iostream>
using std::cout;
using std::endl;
int main()
{
int num1 = 5;
int num2 = 4;
cout << num1+num2 << endl; //outputs 9
cout << num1-num2 << endl; //outputs 1
cout << num1*num2 << endl; //outputs 20
cout << num1/num2 << endl; //outputs 1
cout << num1%num2 << endl; //outputs 1
return 0;
}
The only operator that may throw you off here is the modulus (%) operator. Modulus returns the remainder of
a division result. Both of our numbers, num1 and num2, were declared as integers. Integers are whole numbers,
and do not use decimal points. Four into five goes one time, with one left over as the remainder. That is why
5/4 and 5%4 both output 1. Other modulus outcomes:
6 % 2 = 0
12 % 10 = 2
100 % 70 = 30
Integer vs. Float division
Notice that above that when we divided five by four we got a whole number as the result. That's because we were
working with two whole numbers. What if you want a more accurate result? Floating point division is the answer.
Let's take a look at another program
#include "stdafx.h"
//#using <mscorlib.dll>
using std::cout;
using std::endl;
int main()
{
float num1 = 5;
float num2 = 4;
cout << num1/num2 << endl; //outputs 1.25
int num3 = 5;
float num4 = 4;
cout << num3/num4 << endl; //outputs 1.25
return 0;
}
Notice now that num1 and num2 have both been declared as type 'float.' This allows for decimal point precision.
The answer is now 1.25 instead of one. Also notice that when one variable is an integer and one is a float, you
will still get an answer in float form. Lastly, keep in mind that modulus cannot be used with floating point numbers.
Storing the result
Storing the result is a simple process. In the previous examples, although we did calculations with num1-num4, none of
their original values were changed. To store the result just create an additional variable.
int num1 = 10;
int num2 = 5;
int answer;
answer = num1 + num2;
cout << answer << endl; //outputs 15
In this case num1 and num2 both remain their original values, but 'answer' now holds their sum. An important thing to keep in
mind is that doing any kind of floating point math and storing it in an integer will result in a truncated number.
float num3 = 20;
float num4 = 12;
int answer;
answer = num3/num4;
cout << answer << endl; //outputs 1
Order of Operations
I'm not going to cover this in detail, but it's worth noting that all calculations in C++ follow the same order
of operations that you learned in school.
int answer = 5 + 6 * 3 / 2 + 4 * 2 - 3;
//answer is assigned 19
The results can of course be changed by adding parentheses
int answer = (5 + 6) * 3 / (2 + 4) * 2 - 3;
//answer is assigned 7
Changing the original values
The above examples are great if you just want to output an answer or store it in another variable, but what if you
want to change your original values? You can simply reassign a calculation into an existing variable
int num1 = 5;
num1 = num1 + 10
cout << num1 << endl; //outputs 15
This works, but is cumbersome. There is a shortcut avaiable:
int num1 = 5;
num1 += 10;
cout << num1 << endl;
See the "+=" there? You can use it for any of the other arithmetic operators
+=, -=, *=, /=, and %=
What it does is say "take the value on left, perform the given operation on it with the value on the right, and put it
back in inside the value on the left." These operators can come in very handy.
Inrementation and Decrementation.
There is an even quicker shortcut available for when you only want to increase or decrease your original variable by 1.
int num = 5;
num++;
num--;
cout << num << endl; //outputs 5
You can simply use ++ to increment by 1, or -- to decrement by 1. You can put them either before or after the value to be affected.
This is called prefixing and postfixing
num++; (postfixing)
++num; (prefixing)
In the case above, it doesn't matter which you use. Num will be increased by one in both instances. Here's where the difference comes:
int num1;
int num2 = 4;
num1 = ++num2;
cout << "num1 is " << num1 << " and num2 is " << num2 << endl;
//outputs 'num1 is 5 and num2 is 5'
In this example one will be added onto num2, and then assigned to num1. Both variables now contain the value 5
int num1;
int num2 = 4;
num1 = num2++;
cout << "num1 is " << num1 << " and num2 is " << num2 << endl;
//outputs 'num1 is 4 and num2 is 5'
But in this example num2 is assigned to num1, then incremented. num1 contains 4, num2 contains 5.
That wraps up my tutorial on basic arithmetic in C++. Feel free to leave comments and suggestions.