Lesson 4

Arithmetic operators

2

Arithmetic operators are used frequently in order to calculate and process data in your program. You will use them a lot for different purposes and are important to understand.

Let's briefly look at what all operators does and what they look like

+ adds numbers:

- subtracts numbers from each other:

* multiplies values:

/ divides numbers:

% is called the "modulus operator" and it also performs division. However, it only returns the remainder of what's left:

When it comes to arithmetic, it works exactly like it does in maths. We follow the basic rules of mathematics for calculating.

Tip: When calculating, it is often a good habit to use paranthesis, even if the program works without them, to avoid making some basic math mistakes. Sometimes you'll want to type (10 + 3 + 5 + 7) instead of just 10 + 3 + 5 + 7 even if it seems obvious to you that they're the same!

Exercise

Create a program that calculates the avarage value of a bunch of numbers. If your values are $1$, $2$ and $3$, the result should be $2$.

Remember the arithmetic order for how the calculation works. Paranthesis, multiply/division and addition/subtraction.

1. Declare a few variables and set them to a value of your choice

2. Store the calculation in another variable

3. Print out the result!