Section 5a

Integer Math - Addition and Subtraction


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Adding and Subtracting, with integers

First steps. Truthfully, the use of mathematical or arithmetic operations in most programming languages is quite easy. For the most part, you will implement the mathematical operations with the mathematical symbols you have always used. However, there are some issues that must be considered specifically with integer arithmetic.

Consider the following.

int myAge = 19;
int herAge = 22;
int hisAge = 78;
int sumOfAges;
sumOfAges = myAge + herAge + hisAge;

In the above example, three integers are defined as ages, and one is defined as the sum of them. Then the three ages are added together and assigned to the sumOfAges variable. Subtraction is the same, as shown.

int differenceOfAges;
differenceOfAges = hisAge - myAge;

Once again, remember that while the two sides will be equal during a part of the process, the "=" is called an assignment operator since it is taking information from the right side of it, and placing it into the variable on the left side of it (if it seems like this is being repeated quite a bit, it is because it is important).

It's not an equals sign. One of the conditions to make clear is that you are conducting some operations on the right side of the assignment operator, and when you are done, you will be assigning the results to the variable on the left side.

Consider the following.

int totalStudents = 15;
int newStudents = 3;
totalStudents = totalStudents + newStudents;

This operation is commonly conducted, and as your math teacher would tell you, this is not an equation. This is an assignment operation. Once again, this little rule is being repeated, and once again, it is important.

Watch this video, and work through it on your own computer. The problems are very simple but the more you practice now the more comfortable and fluent you will be as the complexity increases.