# Arithmetic Operations

In C++ it is easy to do arithmetic calculations. The result of our calculations can be stored in a variable or can be used directly.

For example these two Arduino programs calculate 2+3 and print the value. The first program uses the return of 2+3 directly and passes that value to the `println` function. The second program first saves the value of 2+3 in an integer value called `sum` and then prints the value of `sum`. While they each perform the same task, the second approach is easier to use and understand with different sums.

``````void setup()
{
Serial.begin(9600);
Serial.println(3+2);
}
void loop()
{
}
``````
``````void setup()
{
Serial.begin(9600);
int sum = 3+2;
Serial.println(sum);
}
void loop()
{
}
``````

# Basic Operators

The following operators are used in C++ to do simple arithmetic calculations:

Operation C++ Operator
Addition `+`
Subtraction `-`
Multiplication `*`
Division `/`

We can use these operators with numbers directly or we can use them with variables.

``````int variable1 = 5;
int variable2 = 10;

int sum = variable1 + variable2;

//sum will be equal to 15
``````

We can also use the same variable that we want the value to be stored in. For example

``````int variable1 = 5;
variable1 = variable1 * 2;
//this line is translated to
//variable1 = 5 * 2;
//as the value of variable1 at that moment was 5
//variable1 will be equal to 10
``````

Mixing types

What happens if we add integer and float variables together?

``````int variableInt = 5;
float variableFloat = 1.23;

int sumInt = variableInt + variableFloat;
float sumFloat = variableInt + variableFloat;
``````

Is there a difference in the value of `sumInt` and `sumFloat`?

``````variableInt + variableFloat;
``````

will result in a float value of 6.23.

However, when we store the float value 6.23, it depends on the type of variable we are storing the number in. If the type is `int` like in `sumInt` then the number will be converted into an integer and the value in `sumInt` will be 6. If the variable is a `float` like `sumFloat` then the number can be stored as-is. And the value of `sumFloat` will be 6.23.

# Dividing by an Integer

In C++ if we divide an integer by another integer then the result will be an integer value as well. For example:

The output will be 2 and not 2.5

`myVariable` is an integer and so is 2 so the result of the division will be an integer as well. To fix that, we can divide by a real number instead of an integer. `2.0` is the same value as `2` but in C++ there is a difference. `2` has an integer type and `2.0` has a double type.

What is the double type?

The variable type `double` is similar to `float` but it can store a real number with higher precision than `float` by storing more significant digits.

Similar to how we can declare a variable with the `float` type

``````float myFloat = 1.2;
``````

we can declare a `double` type

``````double myDouble = 1.2;
``````

# Order of Operations and Parentheses

The order of operations in C++ follows the concept of precedence in arithmetics. For example multiplication has higher precedence than addition.

If two operators are the same precedence then the expression will be evaluated from left to right.

``````int var = 8 * 2 + 3; // 19
float var2 = 8 * 2.3 / 2; // 9.20
int var3 = 8 / 2 / 4;   // 1
``````

We can also use parentheses to evaluate arithmetic operations before the full expression is evaluated.

``````int var = 8 * (2 + 3); // 8 * 5 = 40*
int var2 = (2 + 3) * ( (7 * 2) - 15); // -5
``````

# Modulo Operator

The modulo operator is represented by `%`, it is an arithmetic operator that returns the remainder of an integer division.

``````int remainder = 10 % 4; // 2
``````

Exercise

Run the following example and see if you can determine a correlation between the output and the numbers used.

If a number is divisible by 2 then the remainder will be 0. And if it is not divisible by 2 then the remainder will be 1. If we want to check if an integer is even or odd, we can calculate number % 2 and check if the answer is 1 or 0.

# Compound Assignment

We have already seen how to assign a value to a variable:

``````int var = 2;
``````

And we have also seen how we can use the variable to do arithmetic operation on it and then save it to the same variable:

``````int var = 2;
var = var + 2; // var is 4
``````

We can also use compound assignments in C++ to apply operations on the variable itself.

``````int var = 2;
``````

The following are compound assignment operators:

Operator Example
`+=` `var += 3;` Add 3 to variable `var`

The value of `var` is now 5
`-=` `var -= 3;` Subtract 3 from variable `var`

The value of `var` is now -1
`*=` `var *= 3;` Multiply `var` by 3

The value of `var` is now 6.
`/=` `var /= 2;` Divide 2 by `var`

The value of `var` is now 1.

# Increment & Decrement

Incrementing means adding 1 to a variable and decrementing is subtracting by 1. We can do this simply with the basic operators that we have learned:

``````int var = 2;
var = var + 1; // var is 3
``````

However we can also use the increment and decrement operators:

Operator Example
`++` `var++;` Increment `var`

The value of `var` is now 3
`--` `var--;` Decrement `var`

The value of `var` is now 1

Homework

Modify the program below to increment `var` at every loop iteration before printing the value of `var`.