Tuesday, March 30, 2010

Using Operates:

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.

Outputting Numeric Values:

Before we begin the examination of the arithmetic operators we will take a brief aside. So far in the C# Fundamentals tutorial there has been no discussion of outputting numeric values to the screen. As we are about to start modifying the values in variables, it is important to be able to view the results of the operations.

In the first part of the C# Fundamentals tutorial, a console application was created and the console was outputted to using the Console.WriteLine command. The Console.WriteLine command may also be used to output numeric data types.
For example:

int quantity = 5;
Console.WriteLine(quantity); // Outputs 5

You can use the Console.WriteLine command to test the examples provided in the following sections

Basic Arithmetic Operators:

There are five basic arithmetic operators available to the C# developer. These are used for addition (+), subtraction (-), multiplication (*), division (/) and modulus (%). Of these, the first four are available to almost every programming language.
Here are some simple examples:

int a = 6;
int b = 3;
int result;

result = a + b; // result = 9
result = a - b; // result = 3
result = a * b; // result = 18
result = a / b; // result = 2
result = a + b - 1; // result = 8
It is important to remember that the resultant value of a mathematical operation is subject to the rules of the receiving variable's data type. The result of a division operation may yield a floating point value. However, if this is assigned to an integer then the fractional part will be truncated. Equally important, and less obvious, is the effect of an operation performed on several integers and assigned to a non-integer. In this case, the result is calculated as an integer before being implicitly converted. This means that although the resultant value is assigned to a floating point variable, the fractional part is still truncated unless at least one of the values is explicitly converted first.
The following examples illustrate this:

int a = 7;
int b = 3;
int integerResult;
float floatResult;

integerResult = a / b; // integerResult = 2 (truncated)
floatResult = a / b; // floatResult = 2.0 (truncated)
floatResult = (float)a / b; // floatResult = 2.33333325

The last of the basic arithmetic operators is modulus. The modulus operator yields a value equal to the remainder of an integer division. As an example, when ten is divided by three the answer is three with a remainder of one. One is the modulus value. The modulus value can be used with both integer and floating point data types

int a = 10;
int b = 3;
float c = 3.5F;
int integerResult;
float floatResult;

integerResult = a % b; // integerResult = 1
floatResult = a % b; // floatResult = 1.0
floatResult = a % c; // floatResult = 3.0

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 the decrement operator (--). The increment and decrement 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) for similar results.

// 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 give the impression that the prefix and postfix varieties of the increment and decrement 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 original value of the variable is used.

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;
Operator Precedence:

All of 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 individual 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 for the most important 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 table. 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 allows modification of 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. The 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

No comments:

Post a Comment