C# Arithmetic Operators
Assume we define two int variables A=15 and B=5 in the examples below.
Operator | Usage | Description | Examples |
---|---|---|---|
+ | A + B | A plus B. | int C = A + B; //C will be 15+5=20 |
- | A - B | Subtract B from A | int C = A - B; //C will be 15-5=10 |
* | A x B | A times B | int C = A * B; //C will be 15X5=75 |
/ | A / B | A divided by B | int C = A / B; //C will be 15/5=3 |
% | A % B | A mod B returns the reminder of A divided by B. | int C = A % B; //C will be 15 % 5=0 |
++ | A++ | return the value of the variable before it adds 1. | int C = A++; //C will be 15 and A will be 16 |
++ | ++A | return the value of the variable after it adds 1. | int C = ++A; //A will be 16 and C will be 16 too. |
-- | A-- | return the value of the variable before it minus 1. | int C = A--; //C will be 15 and A will be 14 |
-- | --A | return the value of the variable after it minus 1. | int C = --A; //A will be 14 and C will be 14 too. |
Example 01-08-01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System; namespace DivisionMod { class Program { static void Main(string[] args) { int i, j; i = 60/7; // 8 => i j = 60%7; // 4 => j Console.WriteLine("i={0}, j={1}", i, j); double x = 10.0D, y; x = x/0.25; // 40.0 => x y = x%9; // 4.0 => y Console.WriteLine("x={0}, y={1}", x, y); Console.Read(); } } }
Output
i=8, j=4 x=40, y=4
- Line 9: Declare 2 int variables i and j.
- Line 10: This is integer division and the quotient is also an integer. So it is 8 and the reminder is neglected
- Line 11: Instead of neglecting the reminder, % operater is used to get the reminder. The arithmetic formula is like this 60=7x8+4. So it is 4.
- Line 12: Output i and j.
- Line 14: Declare 2 double variables x, y and set 10.0D to x.
- Line 15: The result is double-typed 40 and x is assigned the result.
- Line 16: 40%9=4. So the result is double-typed 4.
- Line 17: Output x and y.
Example 01-08-02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System; namespace PlusPlus { class Program { static void Main(string[] args) { int i=5, j, k; j = 2 + i++; // j = 7 and then i = 6 k = 2 + ++i; // i = 7 then k = 2 + 7 = 9 Console.WriteLine("i={0}, j={1}, k={2}", i, j, k); float x = 4.15F, y, z; y = x-- + 1; // 4.15 + 1 = 5.15 => y then 4.15 - 1 = 3.15 => x z = --x + 1; // 3.15 - 1 = 2.15 => x then 2.15 + 1 = 3.15 => z Console.WriteLine("x={0}, y={1}, z={2}", x, y, z); Console.Read(); } } }
Output
i=7, j=7, k=9 x=2.15, y=5.15, z=3.15
- Line 9: Declare 3 int variables i, j and k. Set initial value 5 to i.
- Line 10: We'll calculate the 2 + i first then i++. This statement equals to the following.
- Line 11: We'll calculate ++i first and then the assignment. This statement equals to the following.
- Line 12: Output i, j, k.
- Line 14: Declare 3 float variables x, y, z and set 4.15F to x.
- Line 15: The assignment is calcualted first. So y = x + 1 = 5.15F. Then x-- makes x = x - 1 = 3.15F.
- Line 16: Firstly --x makes x = x - 1 = 2.15F. Then z = x + 1 = 3.15F.
- Line 17: Output x, y, z.
j = 2 + i; i = i + 1;
i = i + 1; k = 2 + i;
In summary, a prefix increment or decrement operation, like ++x or --x, will plus 1 or minus 1 first then calculate the expression and return the value. a postfix increment or decrement operation, like x++ or x-- , will calculate the expression first, then return the result, at last the variable will plus 1 or minus 1.