OPERATORS IN C SHARP

An operator is a symbol (e.g., =, +, >, etc.) that causes C# to take an action. That action might be an assignment of a value to a variable, the addition of two values, or a comparison of two values, etc.

The single equal sign (=) is used to assign a value to a variable, in this case the value 15 to the variable myVariable:

count = 15;

More sophisticated operators, such as the greater-than comparison operator (>) used to compare two values:

if ( valueOne > valueTwo )

The preceding if statement compares valueOne with valueTwo; if the former is larger than the latter, the test evaluates true, and the if statement executes.

Assignment Operator (=) :

The assignment operator causes the operand on the left side of the operator to have its value changed to whatever is on the right side of the operator.

Mathematical Operators

C# uses five mathematical operators, four for standard calculations and one to return the remainder when dividing integers. The following sections consider the use of these operators.

Simple Arithmetical Operators (+, -, *, / ) :

C# offers operators for simple arithmetic: the addition (+), subtraction (-), multiplication (*), and division (/) operators work as you might expect, with the possible exception of integer division.

When you divide two integers, C# divides like a child in the fourth grade: it throws away any fractional remainder. Thus, dividing 17 by 4 returns a value of 4 (17/4 = 4, with C# discarding the remainder of 1).

This limitation is specific to integer division. If you do not want the fractional part thrown away, you can use one of the types that support decimal values, such as float and double. Division between two floats (using the / operator) returns a fractional answer.

Using the modulus Operator to Return Remainders :

To find the remainder in integer division, use the modulus operator (%). For example, the statement 17%4 returns 1 (the remainder after integer division).

The modulus operator turns out to be more useful than you might at first imagine. When you perform modulus n on a number that is a multiple of n, the result is zero. Thus 80%10=0 because 80 is an even multiple of 10.

Increment and Decrement Operators :

A common requirement is to add a value to a variable, subtract a value from a variable, or otherwise change the mathematical value, and then to assign that new value back to the original variable.

Calculate and Reassign Operators:

The need to perform this kind of manipulation is so common that C# includes special operators for self-assignment. Among these operators are +=, -=, *=, /=, and %=, which, respectively, combine addition, subtraction, multiplication, division, and modulus, with self-assignment.

These three instructions, respectively, increment mySalary by 5000, multiply mySalary by 5000, and subtract 5000 from the mySalary variable.

Increment or Decrement by 1:

Because incrementing and decrementing by exactly 1 is a very common need, C# provides two additional special operators for these purposes: increment (++) and decrement (--).

Thus, if you want to increment the variable myAge by 1 you can write:

myAge++;

The Prefix and Postfix Operators:

To complicate matters further, you might want to increment a variable and assign the results to a second variable:

resultingValue = originalValue++;

The question arises: do you want to assign before you increment the value or after? In other words, if originalValue starts out with the value 10, do you want to end with both resultingValue and originalValue equal to 11, or do you want resultingValue to be equal to 10 (the original value) and originalValue to be equal to 11?

C# offer two specialized ways to use the increment and decrement operators: prefix and postfix. The way you use the ++ operator determines the order in which the increment/decrement and assignment take place. The semantics of the prefix increment operator is "increment the original value, and then assign the incremented value to result" while the semantics of the postfix increment operator is "assign the original value to result, and then increment original."

To use the prefix operator to increment, place the ++ symbol before the variable name; to use the postfix operator to increment, place the ++ symbol after the variable name.

Relational Operators :

Relational operators compare two values and then return a Boolean value (true or false). The greater-than operator (>), for example, returns true if the value on the left of the operator is greater than the value on the right. Thus, 5>2 returns the value true, while 2>5 returns the value false.

Equals ==

Not Equals !=

Greater than >

Greater than or Equal to >=

Less than < class="docText">Each of these relational operators acts as you might expect. Notice that most of these operators are composed of two characters. For example, the greater than or equal to operator (>=) is created with the greater than symbol (>) and the equal sign (=). Notice also that the equals operator is created with two equal signs (==) because the single equal sign alone (=) is reserved for the assignment operator.

The C# equals operator (==) tests for equality between the objects on either side of the operator. This operator evaluates to a Boolean value (true or false). Thus, the statement:

myX == 5;

evaluates to true if and only if the myX variable has a value of 5.

Use of Logical Operators with Conditionals :

If statements test whether a condition is true. Often you will want to test whether two conditions are both true, only one is true, or neither is true. C# provides a set of logical operators for this.

The Conditional Operator :

Although most operators are unary (require one term, e.g., myValue++) or binary (two terms, e.g., a+b), there is one ternary (three terms) operator: the conditional operator (?:).

cond-expr  ?  expression1  :  expression2 

This operator evaluates a conditional expression (an expression that returns a value of type bool) and then invokes either expression1 if the value returned from the conditional expression is true, or expression2 if the value returned is false. The logic is: "if this is true, do the first; otherwise do the second.

Short-Circuit Evaluation :

The if statement here is a bit complicated. The entire if statement is in parentheses, as are all if statements in C#. Thus, everything within the outer set of parentheses must evaluate true for the if statement to be true.

Within the outer parentheses are two expressions, (x == 8) and (y == 12), which are separated by an or operator (||). Because x is 8, the first term (x == 8) evaluates true. There is no need to evaluate the second term (y == 12).

Again, there is no need to evaluate the second term. Because the first term is false, the and must fail. (Remember, for an and statement to evaluate true, both tested expressions must evaluate true.)

In cases such as these, the C# compiler will short-circuit the evaluation; the second test will never be performed.

Operator Precedence :

The compiler must know the order in which to evaluate a series of operators.

there are three operators for the compiler to evaluate (=, +, and *). It could, for example, operate left to right, which would assign the value 5 to myVariable, then add 7 to the 5 (12) and multiply by 3 (36) — but of course then it would throw that 36 away. This is clearly not what is intended.

The rules of precedence tell the compiler which operators to evaluate first. As is the case in algebra, multiplication has higher precedence than addition, so 5+7*3 is equal to 26 rather than 36. Both addition and multiplication have higher precedence than assignment, so the compiler will do the math and then assign the result (26) to myVariable only after the math is completed.

Grouping the elements of the assignment in this way causes the compiler to add 5+7, multiply the result by 3, and then assign that value (36) to myVariable.
related posts

No comments:

Post a Comment