BlackWaspTM
C# Programming
.NET 1.1+

C# Arithmetic Operators (2)

The sixth part of the C# Fundamentals tutorial describes the basic arithmetic operators available to the C# programmer. These operators allow general algebraic operations to be carried out against numeric data type values.

Increment and Decrement Operators

C# provides two operators that can be used to increase or decrease a variable's value by one. These are the increment operator (++) and decrement operator (--). These operators are known as unary operators because they are applied to a single value. The operators can be placed before the variable to be adjusted (prefix) or after it (postfix).

// All of these commands add one to the variable "a"
a = a + 1;
++a;        // prefix
a++;        // postfix

// All of these commands subtract one from the variable "a"
a = a - 1;
--a;
a--;

The above examples imply that the prefix and postfix varieties of the operators yield the same results. This is true in the simple scenarios examined so far. However, when the operators are used as a part of a more complex expression there is an important difference. Where the prefix variation is used, the increment or decrement operation is applied before the value of the variable is applied to the rest of the calculation. Where the postfix version is used, the value is changed afterwards.

int a;
int b;

// Prefix.  a is incremented before its value is assigned to b
a = 10;
b = ++a;        //a = 11, b = 11;

// Postfix.  a is incremented after its value is assigned to b
a = 10;
b = a++;        //a = 11, b = 10;

Operator Precedence

The operators discussed in this article may be used together in a single expression. The result of the expression is dependant upon the order in which the operators are applied. This order is determined by the operator precedence, which applies an importance to each operator.

Over the next few articles we will build a table of the operator precedence of C# operators. For the operators described so far, the order of precedence is as follows:

Increment / Decrement Operators
++(postfix) --(postfix) ++(prefix) --(prefix)
Basic Arithmetic Operators
* / % + -

It is important to understand the effect of the operator precedence rules. The following code gives examples of how the importance of an operator affects the end result of an expression:

int a = 10;
int b = 6;
int c = 2;
int result;

// * is applied before +
result = a + b * c;             //result = 22

// / is applied before -
result = a - b / c;             //result = 7

// Order of precedence is *, /, +, -
result = a * b + b / c - 1;     //result = 62

Parentheses Operator

The parentheses operator you to control the order of precedence for operators. By surrounding parts of an expression with parentheses, the precedence of this part of the expression is increased to be of higher importance than items outside of the parentheses. Parentheses may also be nested. The order in which the operators are applied in the previous examples can therefore be modified.

int a = 10;
int b = 6;
int c = 2;
int result;

result = (a + b) * c;             //result = 32

result = (a - b) / c;             //result = 2

result = a * (b + b / (c - 1));   //result = 120
19 August 2006