
.NET 0

Lecture1.1

Lecture1.2

Lecture1.3

Lecture1.4

Lecture1.5

Lecture1.6


Tools 0

Lecture2.1


Data Types and Variables 0

Lecture3.1

Lecture3.2

Lecture3.3

Lecture3.4


Operators 0

Lecture4.1


Object Oriented Programming 0

Lecture5.1

Lecture5.2

Lecture5.3

Lecture5.4


Exception Handling 0

Lecture6.1


Graphics 0

Lecture7.1


WPF 0

Lecture8.1


XML 0

Lecture9.1


HTTP 0

Lecture10.1

Lecture10.2

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 builtin 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
## [](#incrementanddecrement)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("POSTINCREMENT = {0}", a++);
Console.WriteLine("PREINCREMENT = {0}", ++b);
Console.WriteLine("POSTDECREMENT = {0}", c);
Console.WriteLine("PREDECREMENT = {0}", d);
Console.Read();
}
}
}
RESULT
POSTINCREMENT = 99
PREINCREMENT = 100
POSTDECREMENT = 99
PREDECREMENT = 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.