C# Tutorial – Arithmetic operators 2

Arithmetic operators

facebooktwittergoogle_pluslinkedinmail

Sooner or later every programmer will have to do some operations – arithmetic, logical, etc. C# has a lot of operators to help with that. And this time I’d like to introduce arithmetic operators.

Basic arithmetic operators

The basic one are know to everyone from math lessons.

            int a = 4;
            int b = 2;
            int result = a + b; // 6
            result = a - b; // 2
            result = a * b; // 8
            result = a / b; // 2

But the result of an operation isn’t as straight forward every time.

            int a = 5;
            int b = 3;
            int result = a / b; // ?

As you know the int  variable contains only integer values – no information about the fractional part. Can you guess the value of the result variable? Will it be rounded to 2 or will it be cut to 1? The result will be 1 – it’s because the fractional part is just lost, there is no rounding in integer arithmetic.

Reminder operator

There is one more arithmetic operator – remainder.

            int a = 5;
            int b = 3;
            int result = a % b; // 2

In integer arithmetic, the a % b  get the value of a – (a / b) * b . You have to remember that in integer arithmetic a / b * b  is not always equal to a .

a = 5
b = 3
a % b = a - (a / b) * b = 5 - (5 / 3) * 3 = 5- 1 * 3 = 5 – 3 = 2

Increment and decrement operators

There are operators that can increment or decrement a variable.

            int a = 5;
            a++;
            // a = 6

            int b = 3;
            b--;
            // b = 2

Another way to do it:

            int a = 5;
            ++a;
            // a = 6

            int b = 3;
            --b;
            // b = 2

You might wonder what’s the difference between a++  and ++a ? In above cases they do exactly the same, but let’s look at this case:

            int a = 5;
            int b = a++; // b = 5

            a = 5;
            int c = ++a; // c = 6

Let’s analyze it. The first case:

            int a = 5;
            int b = a++;
            // First value of variable a (5) is returned to the expression, so b = 5
            // Next variable a is incremented, a = a + 1 = 6

The second case:

            int a = 5;
            int b = ++a;
            // First variable a is incremented, so a = a + 1 = 6
            // Next value of variable a (6) is returned to the expression, b = 6

It works the same with decrement operator.

            int a = 5;
            int b = a--; // b = 5

            a = 5;
            int c = --a; // c = 4

Hope it’s clear.

 

facebooktwittergoogle_pluslinkedinmail

Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “C# Tutorial – Arithmetic operators

  • quest bars

    When someone writes an article he/she retains the thought of a user in his/her brain that how a user
    can know it. Thus that’s why this paragraph is outstdanding.
    Thanks!