Skip to main content

Arithmetic operations

C# uses most of the operations that are used in other programming languages as well. Operations represent certain actions on operands - participants of the operation. An operand can be a variable or some value (for example, a number). Operations can be unary (performed on one operand), binary (performed on two operands) and ternary (performed on three operands). Let's consider all types of operations.

Binary arithmetic operations:

  • +

The operation of adding two numbers:

int x = 10;
int z = x + 12; // 22
  • -

The operation of subtracting two numbers:

int x = 10;
int z = x - 6; // 4
  • *

The operation of multiplying two numbers:

int x = 10;
int z = x * 5; // 50
  • /

the operation of dividing two numbers:

int x = 10;
int z = x / 5; // 2

double a = 10;
double b = 3;
double c = a / b; // 3.33333333

When dividing, note that if both operands are integers, the result will also be rounded to integers:

double z = 10 / 4; //result is 2.

Despite the fact that the result of the operation is finally placed in a variable of double type, which allows you to save the fractional part, but the operation itself involves two literals, which by default are treated as int objects, i.e. integers, and the result will also be integer.

To get out of this situation, it is necessary to define literals or variables participating in the operation as double or float types:

double z = 10.0 / 4.0; //result is equal to 2.5
  • %

Operation to get the remainder from integer division of two numbers:

double x = 10.0;
double z = x % 4.0; //result is 2.

There are also a number of unary operations that involve a single operand:

  • ++

Increment operation

Increment can be prefixed: ++x - first the value of variable x is incremented by 1, and then its value is returned as the result of the operation.

And there is also a postfix increment: x++ - first the value of variable x is returned as the result of the operation and then 1 is added to it.

int x1 = 5;
int z1 = ++x1; // z1=6; x1=6

int x2 = 5;
int z2 = x2++; // z2=5; x2=6
  • --

A decrement operation, or decrementing a value by one. There is also a prefix form of decrement (--x) and a postfix form (x--).

int x1 = 5;
int z1 = --x1; // z1=4; x1=4

int x2 = 5;
int z2 = x2--; // z2=5; x2=4

When performing several arithmetic operations at once, the order in which they are performed should be considered. Prioritize operations from highest to lowest:

  1. Increment, decrement

  2. Multiplication, division, obtaining a remainder

  3. Addition, subtraction

Brackets are used to change the order of operations.

Consider a set of operations:

int a = 3;
int b = 5;
int c = 40;
int d = c---b*a; // a=3 b=5 c=39 d=25

Here we are dealing with three operations: decrement, subtraction and multiplication. The variable c is decremented first, then multiplication b*a, and finally subtraction. So actually the set of operations looked like this:

int d = (c--)-(b*a);

But with the help of parentheses we could change the order of operations, for example, as follows:

int a = 3;
int b = 5;
int c = 40;
int d = (c-(--b))*a; // a=3 b=4 c=40 d=108

Operator associativity

As noted above, the multiplication and division operations have the same precedence, but what then is the result in the expression:

int x = 10 / 5 * 2;

Should we treat this expression as (10 / 5) * 2 or as 10 / (5 * 2)? Because we will get different results depending on the interpretation.

When operations have the same priority, the order of computation is determined by the associativity of the operators. There are two types of operators depending on associativity:

Left-associative operators, which are executed from left to right

Right-associative operators, which are executed from right to left

All arithmetic operators are left-associative, that is, they are executed from left to right. Therefore, the expression 10 / 5 * 2 should be interpreted as (10 / 5) * 2, i.e. the result is 4.