Arithmetic Operations

This section describes arithmetic operations: Multiplication, Division, Addition, and Subtraction.

So far we have learned how to enter data, store data, and retrieve data. The next step is to learn how to operate data, like how to add two values together to get the sum. This requires the help of data operations. Let's start with arithmetic operations first. Other types of operations will be discussed in later chapters.

There are 4 basic arithmetic operations that we can perform on integral and real data. Each of them is represented by special symbol called operator. Bellow is the operators of the 4 arithmetic operations:

• "*": Multiplication
• "/": Division
• "-": Subtraction

A single arithmetic operation can be entered into C# statements in a format called arithmetic expression, with the following syntax:

```arithmetic_expression:
literal arithmetic_operator literal
```

Note that the literals used in an arithmetic expression must be of the integral or real data type. For example: 1 + 2, 1.4142 * 1.4142, 10 / 3.

We can also put variables instead of literals into arithmetic expression, as long as the variables are of integral or real data type. For example: 3.14159*diameter, sqrt_s*sqrt_s. Note that space characters before or after the operator will be ignored.

When a statement with an arithmetic express is executed, the expression will be evaluated by performing the arithmetic operation, and the resulting value will be placed into the statement where the expression is located. For example, the following statements will store the value 3 to a, and 15 to b.

```   long a, b;
a = 1 + 2;
b = 5*a;
```

Multiple arithmetic operations can also be entered into C# statements as an arithmetic expression with a more complex syntax:

```arithmetic_expression:
literal arithmetic_operator literal arithmetic_operator literal ...
```

For example: 5*1+2, pi*r*r, 199.99*qty+shipping_fee.

Rule: When multiple operators are used in an expression, data operation must be started with the operator that has the highest level of precedence. If two operators have the same level of precedence, starts with the left operator first. If an operator with a lower level of precedence needs to be processed first, enclose this operator and the related data in a pair of parentheses.

Levels of precedence of arithmetic operators:

• Multiplicative level - high: * / %
• Additive level - low: + -

Examples of expressions:

• "1 + 2": This expression is also simple. It has only one operator. The resulting value is 3.
• "d*3.14159": This is another one-operator expression. The resulting value is the value of variable d multiply by 3.14159.
• "price + price*tax_rate": This is a two-operator expression. The * operator has a higher precedence, so the computer will calculate price*tax_rate first, and takes the result and add it to price.
• "price*(1+tax_rate)": This is also a two-operator expression. The parentheses forces the computer to calculate 1+tax_rate first, and takes the result and multiply it to price.

So now we know that an arithmetic expression can have one or many arithmetic operations. It can also have parentheses to change alter the order evaluation process. At the end of the evaluation process, an arithmetic express will always produce an integer or real value. Base on this observation, we can also call a single literal or variable of integral or real data type as an arithmetic expression, because it is also producing an integer or real value. So we can expend our arithmetic expression definition with the following syntax:

```arithmetic_expression:
literal
variable
```

With the understanding that an arithmetic expression always produces an integer or real value, we can now generize our definition of assignment statement to take an arithmetic expression at the right hand side of the assignment sign "=", with following syntax:

```assignment statement:
integer_variable = arithmetic_expression
real_variable = arithmetic_expression
```