Intro


C# Arithmetic Operators

C# defines the following arithmetoc operators:

Arithmetic Operator Meaning
+ Addition
- Subtract
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement

+,-,* and / all work as you would expect them to. They cab be used with any built-in numeric data type.
When the / is applied to integers, any remainder gets truncated e.g 9/4 results to 2.

To obtain the remainder of the division you use the modulus operator,%. This operator can also be called remainder operator. e.g 9 % 4 results to 1. The modulus operator can be used with both floating point and integer numbers.

EXAMPLE ```c# using System;

namespace ArithmeticApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Addition : 9 + 4 = {0}", 9 + 4);
            Console.WriteLine("Substraction : 9 - 4 = {0}", 9 - 4);
            Console.WriteLine("Multiplication : 9 * 4 = {0}", 9 * 4);
            Console.WriteLine("Division : 9 / 4 = {0}", 9 / 4);
            Console.WriteLine("Modulus : 9 % 4 = {0}", 9 % 4);
            Console.Read();
        }
    }
}
RESULT
Addition : 9 + 4 = 13
Substraction : 9 - 4 = 5
Multiplication : 9 * 4 = 36
Division : 9 / 4 = 2
Modulus : 9 % 4 = 1
## [](#increment-and-decrement)Increment and Decrement

The `++` and the `--` are the _increment_ and _decrement_ operators respectively.  
The increment operator adds `1` to its operand while the decrement operator subtracts `1` from its operand.  
For instance:  
`i = i + 1` is the same as `i++`.  
And :  
`i = i - 1` is the same as `i--`.

The increment and decrement operators can either _prefix_ or _postfix_ the operand.
```c#
    i = i + 1

can be written as
PREFIX ```c# ++i;

or  
POSTFIX
```c#
x++;

Clearly if we were to run the above example, there is no difference between the two forms of increment operators.
However, when these operators are used in part of a larger expression, there is a significant difference.
When these operators precede their operand, the result of the operation is the value of the operand after the increment/decrement.
If it follows their operand, the result of the operation is the value of the operand before the increment/decrement.

EXAMPLE ```c# using System;

namespace ArithmeticApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 99,b =99,c = 99, d = 99;
            Console.WriteLine("POST-INCREMENT = {0}", a++);
            Console.WriteLine("PRE-INCREMENT = {0}", ++b);
            Console.WriteLine("POST-DECREMENT = {0}", c--);
            Console.WriteLine("PRE-DECREMENT = {0}", --d);
            Console.Read();
        }
    }
}
RESULT
POST-INCREMENT = 99
PRE-INCREMENT = 100
POST-DECREMENT = 99
PRE-DECREMENT = 98

So here's the deal:

*   `a++` : First the value of `a` is retrieved. Then it's incremented. However, the original value of `a` is what is returned. In this case `99`.
*   `++b` : Fisrt the value of `b` is incremented. Then it's retrieved and returned. So in this case `1` is added to `99` then the result,`100` is returned.
*   `c++` : First the value of `c` is retrieved. Then it's decremented. However, the original value of `c` is what is returned. In this case `99`.
*   `d--` : Fisrt the value of `d` is decremented. Then it's retrieved and returned. So in this case `1` is subtracted from `99` then the result,`98` is returned.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post