Introduction to Operators and Operands
Introduction to Operators and Operands
Fundamental C# Operators
Introduction to Operators
An operation is an action performed on one or more values either to modify the value held by one or both of the variables, or to produce a new value by combining existing values. Therefore, an operation is performed using at least one symbol and at least one value. The symbol used in an operation is called an operator. A value involved in an operation is called an operand.
Introduction to Unary Operators
A unary operator is an operator that performs its operation on one operand. An operator is referred to as binary if it operates on two operands.
Practical Learning: Introducing Operators and Operands
<!DOCTYPE html> <html> <head> <title>Water Distribution Company</title> </head> <body> <div align="center"> <h2>Water Distribution Company</h2> <h3>Bill Preparation</h3> </div> @{ decimal distributionCharges = 1000; decimal environmentCharges = 1200; decimal totalCharges = 200; } <div align="center"> <form name="fromBillPreparation" method="post"> <table> <tr> <td style="width: 200px; font-weight: bold">Distribution Charges:</td> <td><input type="text" name="txtDistributionCharges" value="@distributionCharges" /></td> </tr> <tr> <td style="font-weight: bold">Environment Charges:</td> <td><input type="text" name="txtEnvironmentCharges" value="@environmentCharges" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="txtCalculate" value="Calculate" /></td> </tr> <tr> <td style="font-weight: bold">Total Charges:</td> <td><input type="text" name="txtTotalCharges" value=@totalCharges /></td> </tr> </table> </form> </div> </body> </html>
The assignment operation, represented with the = symbol, gives a value to a variable or an object. The formula to use it is:
variable-name = value
Here is an example:
@{
// Declaring and initializing a variable using the assignment operator
decimal salary = 12.55M;
}
We saw how to declare various variables that use the same data type but separating their names with commas. When doing this, you can also initialize each variable by assigning it the desired value before the comma or the semi-colon. Here is an example:
@{
// Initializing various variables when declaring them with the same data type
decimal value1 = 224.58m, value2 = 1548.26m;
}
In order to express a number lower than 0, it must be appended with a sign, namely the - symbol. Examples are -12, -448, or -32706. A value accompanied by - is referred to as negative. The - sign must be typed on the left side of the number it is used to negate. If a number does not have a sign, it is considered positive. Therefore, whenever a number is negative, it must have a - symbol. In the same way, if you want to change a value from positive to negative, you can just add a - sign to its left.
Introduction
The addition is used to add two values of the same type one to another, as many as necessary. The addition is performed in mathematics using the + (plus) symbol. The same symbol is used in C#.
Adding Two Numbers
To get the addition of two values, you add the first one to the other. After the addition of two values has been performed, you get a new value. You can also add more than two values, like a + b + c. You can also add some values already declared and initialized in Your program. You can also get the values from the user.
Practical Learning: Using the Addition Operator
<!DOCTYPE html> <html> <head> <title>Water Distribution Company</title> </head> <body> <div align="center"> <h2>Water Distribution Company</h2> <h3>Bill Preparation</h3> </div> @{ decimal distributionCharges = 0.00m; decimal environmentCharges = 0.00m; decimal totalCharges = 0.00m; if (IsPost) { distributionCharges = Request["txtDistributionCharges"].AsDecimal(); environmentCharges = Request["txtEnvironmentCharges"].AsDecimal(); totalCharges = distributionCharges + environmentCharges; } } <div align="center"> <form name="fromBillPreparation" method="post"> <table> <tr> <td style="width: 200px; font-weight: bold">Distribution Charges:</td> <td><input type="text" name="txtDistributionCharges" value="@distributionCharges" /></td> </tr> <tr> <td style="font-weight: bold">Environment Charges:</td> <td><input type="text" name="txtEnvironmentCharges" value="@environmentCharges" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="txtCalculate" value="Calculate" /></td> </tr> <tr> <td style="font-weight: bold">Total Charges:</td> <td><input type="text" name="txtTotalCharges" value=@totalCharges /></td> </tr> </table> </form> </div> </body> </html>
String Addition
You can also add strings. If you have two constant strings and want to add them, simply use + between them. An example is "Pie" + "Chart". Like the addition of numbers, the addition of strings produces a new string. For example, "Pie" + "Chart" produces "PieChart".
If the strings are stored in variables, you can perform the addition on those variables. Here is an example:
@{
string prefix = "De";
string rest = "Niro";
string name = prefix + rest;
}
This would produce:
DeNiro
Just like you can add various numbers, you can add different strings. Here is an example:
@{
string firstName = "Samuel";
string lastName = "Eto'o";
string suffix = "Fils";
string name = firstName + " " + lastName + ", " + suffix;;
}
This would produce:
Samuel Eto'o, Fils
Incrementing a Variable
To increment a value, add 1 to it. In computer programming, first declare a variable. Add 1 to that variable, and assign the result to the variable itself. This is illustrated in the following example:
@{
int value = 12;
value = value + 1;
}
C# provides a special operator that takes care of this operation. The operator is called the increment operator and is represented by ++. Instead of writing value = value + 1, you can write value++ and you would get the same result. Here is an example:
@{
int value = 12;
value++;
}
The ++ is a unary operator because it operates on only one variable. It is used to modify the value of the variable by adding 1 to it. Every time the value++ executes, the compiler takes the previous value of the variable, adds 1 to it, and the variable holds the incremented value.
Pre and Post-Increment
When using the ++ operator, the position of the operator with regards to the variable it is modifying can be significant. To increment the value of the variable before re-using it, you should position the operator on the left of the variable. Here is an example:
@{
int value = 12;
++value;
}
When writing ++value, the value of the variable is incremented before being called. On the other hand, if you want to increment the variable after calling it, position the increment operator on the right side of the variable. Here is an example:
@{
int value = 12;
value++;
}
Compound Addition
Introduction
It is not unusual to add a constant value to a variable. All you have to do is to declare another variable that would hold the new value. Here is an example:
@{
decimal value = 12.75M;
decimal newValue;
newValue = value + 2.42M;
}
This technique requires that you use an extra variable in your application. If you will not need to keep the original value of the source variable, you can perform the addition directly on the variable by adding the desired value to the variable. This operation modifies whatever value a variable is holding and does not need an additional variable.
To add a value to a variable and change the value that the variable is holding, you can combine the assignment "=" and the addition "+" operators to produce a new operator as +=. Here is an example:
@{
decimal value = 12.75m;
value += 2.42;
}
Compound Addition and Strings
Imagine you want to combine two strings to get a new string. Here are examples:
string name = "Jim";
name = name + "my";
This would produce the name variable as:
Jimmy
Instead of, or besides, the regular addition, you can use the compound addition, practically the same way it is used for numbers. Here is an example:
name += "my";
Introduction
The multiplication is done using the * operator. The operation can be performed on constant values, as in 25 * 66. If the values are stored in variables, the operation can be performed on those variables. Here is an example:
@{
decimal value1 = 224.58m, value2 = 1548.26m;
decimal result = value1 * value2;
}
The operation can involve two or more operands. Just like the addition, the multiplication is associative. This means that a * b * c is the same as c * b * a.
Practical Learning: Using the Multiplication Operator
<!DOCTYPE html> <html> <head> <title>Water Distribution Company</title> </head> <body> <div align="center"> <h2>Water Distribution Company</h2> <h3>Bill Preparation</h3> </div> @{ string strTotalCharges = ""; string strWaterUseCharges = ""; string strSewerUseCharges = ""; string strWaterSewerUsage = ""; string strEnvironmentCharges = ""; string strDistributionCharges = ""; if (IsPost) { int waterSewerUsage = Request["txtWaterSewerUsage"].AsInt(); decimal waterUseCharges = waterSewerUsage * 4.18m; decimal sewerUseCharges = waterSewerUsage * 5.85m; decimal distributionCharges = (waterUseCharges + sewerUseCharges) * 0.14286m; decimal environmentCharges = (waterUseCharges + sewerUseCharges) * 0.057792m; decimal totalCharges = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges; strWaterSewerUsage = waterSewerUsage.ToString("F"); strWaterUseCharges = waterUseCharges.ToString("F"); strSewerUseCharges = sewerUseCharges.ToString("F"); strDistributionCharges = distributionCharges.ToString("F"); strEnvironmentCharges = environmentCharges.ToString("F"); strTotalCharges = totalCharges.ToString("F"); } } <div align="center"> <form name="fromBillPreparation" method="post"> <table style="width: 550px"> <tr> <td style="width: 325px; font-weight: bold">Water and Sewer Usage:</td> <td><input type="text" name="txtWaterSewerUsage" value="@strWaterSewerUsage" /> Gallons</td> </tr> <tr> <td> </td> <td><input type="submit" name="txtCalculate" value="Calculate" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td style="width: 200px; font-weight: bold">Water Use Charges => 4.18 per 1,000 Gallons:</td> <td><input type="text" name="txtWaterUseCharges" value="@strWaterUseCharges" /></td> </tr> <tr> <td style="font-weight: bold">Sewer Use Charges => 5.85 per 1,000 Gallons:</td> <td><input type="text" name="txtSewerUseCharges" value="@strSewerUseCharges" /></td> </tr> <tr> <td style="width: 200px; font-weight: bold">Distribution Charges:</td> <td><input type="text" name="txtDistributionCharges" value="@strDistributionCharges" /></td> </tr> <tr> <td style="font-weight: bold">Environment Charges:</td> <td><input type="text" name="txtEnvironmentCharges" value="@strEnvironmentCharges" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td style="font-weight: bold">Total Charges:</td> <td><input type="text" name="txtTotalCharges" value=@strTotalCharges /></td> </tr> </table> </form> </div> </body> </html>
Compound Multiplication
You can multiply a value by a variable and assign the result to the same variable. Here is an example:
@{
decimal value = 12.75M;
value = value * 2.42m;
}
To make this operation easy, the C# language supports the compound multiplication assignment operator represented as *=. To apply it, use the *= operator and assign the desired value to the variable. Here is an example:
@{
decimal value = 12.75M;
value *= 2.42m;
}
Introduction
The subtraction is performed with the - symbol. Here is an example:
@{
decimal value1 = 224.58m, value2 = 1548.26m;
decimal result = value1 - value2;
}
Unlike the addition, the subtraction is not associative. In other words, a - b - c is not the same as c - b - a.
Practical Learning: Using the Subtraction Operator
<!DOCTYPE html> <html> <head> <title>Water Distribution Company</title> <style> .container { margin: auto; width: 550px } </head> <body> <div class="container"> <h2>Water Distribution Company</h2> <h3>Bill Preparation</h3> </div> @{ string strTotalCharges = ""; string strMeterReadingEnd = ""; string strWaterUseCharges = ""; string strSewerUseCharges = ""; string strWaterSewerUsage = ""; string strMeterReadingStart = ""; string strEnvironmentCharges = ""; string strDistributionCharges = ""; if (IsPost) { int meterReadingStart = Request["txtMeterReadingStart"].AsInt(); int meterReadingEnd = Request["txtMeterReadingEnd"].AsInt(); int waterSewerUsage = meterReadingEnd - meterReadingStart; decimal waterUseCharges = waterSewerUsage * 4.18m; decimal sewerUseCharges = waterSewerUsage * 5.85m; decimal distributionCharges = (waterUseCharges + sewerUseCharges) * 0.14286m; decimal environmentCharges = (waterUseCharges + sewerUseCharges) * 0.057792m; decimal totalCharges = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges; strTotalCharges = totalCharges.ToString("F"); strMeterReadingEnd = meterReadingEnd.ToString(); strWaterSewerUsage = waterSewerUsage.ToString(); strWaterUseCharges = waterUseCharges.ToString("F"); strSewerUseCharges = sewerUseCharges.ToString("F"); strMeterReadingStart = meterReadingStart.ToString(); strEnvironmentCharges = environmentCharges.ToString("F"); strDistributionCharges = distributionCharges.ToString("F"); } } <div class="container"> <form name="fromBillPreparation" method="post"> <table style="width: 550px"> <tr> <td style="width: 325px; font-weight: bold">Meter Reading Start:</td> <td><input type="text" name="txtMeterReadingStart" value="@strMeterReadingStart" /> Gallons</td> </tr> <tr> <td style="width: 325px; font-weight: bold">Meter Reading End:</td> <td><input type="text" name="txtMeterReadingEnd" value="@strMeterReadingEnd" /> Gallons</td> </tr> <tr> <td> </td> <td><input type="submit" name="txtCalculate" value="Calculate" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td style="width: 325px; font-weight: bold">Water and Sewer Usage:</td> <td><input type="text" name="txtWaterSewerUsage" value="@strWaterSewerUsage" /> Gallons</td> </tr> <tr> <td style="width: 200px; font-weight: bold">Water Use Charges => 4.18 per 1,000 Gallons:</td> <td><input type="text" name="txtWaterUseCharges" value="@strWaterUseCharges" /></td> </tr> <tr> <td style="font-weight: bold">Sewer Use Charges => 5.85 per 1,000 Gallons:</td> <td><input type="text" name="txtSewerUseCharges" value="@strSewerUseCharges" /></td> </tr> <tr> <td style="width: 200px; font-weight: bold">Distribution Charges:</td> <td><input type="text" name="txtDistributionCharges" value="@strDistributionCharges" /></td> </tr> <tr> <td style="font-weight: bold">Environment Charges:</td> <td><input type="text" name="txtEnvironmentCharges" value="@strEnvironmentCharges" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td style="font-weight: bold">Total Charges:</td> <td><input type="text" name="txtTotalCharges" value=@strTotalCharges /></td> </tr> </table> </form> </div> </body> </html>
Decrementing a Variable
When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a value. This operation works as if a value is decremented by 1. Here is an example:
@{
int value = 12;
value = value - 1;
}
As done to increment, C# provides a quicker way of subtracting 1 from a value. This is done using the decrement operator, that is --. Here is an example:
@{
int value = 12;
value--;
}
Pre-Decrementing a Value
The position of the -- operator can be important. If you want to decrement the variable before calling it, position the decrement operator on the left side of the operand. Here is an example:
@{
int value = 12;
--value;
}
If you plan to decrement a variable only after it has been accessed, position the operator on the right side of the variable. Here is an example:
@{
int value = 12;
Value--;
}
Compound Subtraction
You may want to subtract a constant value from a variable. To decrement a value from a variable, use the subtraction and apply the same technique. This is done with the -= operator. Here is an example:
@{
decimal value = 12.75m;
value -= 2.42;
}
Introduction
The division is used to get the fraction of one number in terms of another. The division is performed using the forward slash /. When performing the division, be aware of its many rules. Probably the most important is that you should never divide by zero (0).
Practical Learning: Using the Division Operator
<!DOCTYPE html> <html> <head> <title>Water Distribution Company</title> </head> <body> <div align="center"> <h2>Water Distribution Company</h2> <h3>Bill Preparation</h3> </div> @{ string strTotalCharges = ""; string strMeterReadingEnd = ""; string strWaterUseCharges = ""; string strSewerUseCharges = ""; string strWaterSewerUsage = ""; string strMeterReadingStart = ""; string strEnvironmentCharges = ""; string strDistributionCharges = ""; if (IsPost) { int meterReadingStart = Request["txtMeterReadingStart"].AsInt(); int meterReadingEnd = Request["txtMeterReadingEnd"].AsInt(); int waterSewerUsage = meterReadingEnd - meterReadingStart; decimal waterUseCharges = waterSewerUsage * 4.18m / 1000m; decimal sewerUseCharges = waterSewerUsage * 5.85m / 1000m; decimal distributionCharges = (waterUseCharges + sewerUseCharges) * 0.14286m; decimal environmentCharges = (waterUseCharges + sewerUseCharges) * 0.057792m; decimal totalCharges = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges; strTotalCharges = totalCharges.ToString("F"); strMeterReadingEnd = meterReadingEnd.ToString(); strWaterSewerUsage = waterSewerUsage.ToString(); strWaterUseCharges = waterUseCharges.ToString("F"); strSewerUseCharges = sewerUseCharges.ToString("F"); strMeterReadingStart = meterReadingStart.ToString(); strEnvironmentCharges = environmentCharges.ToString("F"); strDistributionCharges = distributionCharges.ToString("F"); } } <div align="center"> <form name="fromBillPreparation" method="post"> <table style="width: 550px"> <tr> <td style="width: 325px; font-weight: bold">Meter Reading Start:</td> <td><input type="text" name="txtMeterReadingStart" value="@strMeterReadingStart" /> Gallons</td> </tr> <tr> <td style="width: 325px; font-weight: bold">Meter Reading End:</td> <td><input type="text" name="txtMeterReadingEnd" value="@strMeterReadingEnd" /> Gallons</td> </tr> <tr> <td> </td> <td><input type="submit" name="txtCalculate" value="Calculate" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td style="width: 325px; font-weight: bold">Water and Sewer Usage:</td> <td><input type="text" name="txtWaterSewerUsage" value="@strWaterSewerUsage" /> Gallons</td> </tr> <tr> <td style="width: 200px; font-weight: bold">Water Use Charges => 4.18 per 1,000 Gallons:</td> <td><input type="text" name="txtWaterUseCharges" value="@strWaterUseCharges" /></td> </tr> <tr> <td style="font-weight: bold">Sewer Use Charges => 5.85 per 1,000 Gallons:</td> <td><input type="text" name="txtSewerUseCharges" value="@strSewerUseCharges" /></td> </tr> <tr> <td style="width: 200px; font-weight: bold">Distribution Charges:</td> <td><input type="text" name="txtDistributionCharges" value="@strDistributionCharges" /></td> </tr> <tr> <td style="font-weight: bold">Environment Charges:</td> <td><input type="text" name="txtEnvironmentCharges" value="@strEnvironmentCharges" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td style="font-weight: bold">Total Charges:</td> <td><input type="text" name="txtTotalCharges" value=@strTotalCharges /></td> </tr> </table> </form> </div> </body> </html>
Compound Division
Remember that you can add, subtract, or multiply a value to a variable and assign the result to the variable itself. You can also perform this operation using the division. Here is an example:
@{
decimal value = 12.75m;
value = value / 2.42m;
}
To perform this operation faster, the C# language provides the /= operator. Here is an example of using it:
@{
decimal value = 12.75m;
value /= 2.42;
}
Introduction
The remainder operation provides the value remaining after a division renders a natural result. The remainder operation is performed with the percent sign (%) which is gotten from pressing Shift + 5.
Here is an example:
@{
int players = 18;
int remainder = players % 11;
}
The Compound Remainder
As seen with the other arithmetic operators, you can find the remainder of a variable and assign the result to the variable itself. Here is an example:
@{
int players = 18;
int rem = players % 11;
}
To support a faster version of this operation, the C# language provides the compound remainder operator represented as %=. To use it, assign its value to the variable. Here is an example:
@{
int players = 18;
players %= 11;
}
Bits Operations
Introduction
A bit is the smallest piecce of information stored in the computer memory. It can be represented as either 1 or 0. Bit manipulation consists of changing the value (1 or 0, or 0 or 1) in a bit.
"Reversing" a Bit
Bit reversal consists of changing the value of a bit. If the bit contains 1, it would become 0. If it contains 0, it would become 1. To support this operation, the C# language provides the bitwise negation operator represented with the ~ symbol.
As an example, consider the number 286. The decimal number 286 converted to binary is 100011110. You can reverse each bit as follows:
To use the bitwise negation operator, type ~ on the left side of the value. Here is an example:
Bitwise Conjunction
Introduction
Bitwise conjunction consists of adding the content of a bit to the content of another bit. To support the bitwise conjunction operation, the C# language provides the & operator.
Compound Bitwise Conjunction
You can perform a bitwise conjunction on a variable and assign the result to a variable. Here is an example:
@{
int number1 = 428;
int number2 = 1606;
int result = (number1 & number2);
}
You can also assign the result to the same variable. Here is an example:
@{
int number = 286;
number = number & 48;
}
Instead of performing the operation on two steps, a shorter version of this operator consists of using the &= operator. Here is an example:
@{
int number = 559;
int result &= number;
}
Bitwise Disjunction
Introduction
Bitwise disjunction consists of disjoining a bit from another bit. To support this operation, the C# language provides the bitwise disjunction operator represented with |.
As an example, consider the number 1608 bit-disjoined to 3175. The decimal number 1608 converted to binary is 110010010000. The decimal number 3175 converted to binary is 101100100101. Based on the above 4 points, we can disjoin these two numbers as follows:
Compound Bitwise Disjunction
You can bitwise-disjoin two values and assign the result to a variable. Here is an example:
@{
int number1 = 8374;
int number2 = 2941;
int result = (number1 | number2);
}
You can disjoin a number of bits on a variable and assign the result to the same variable. Here is an example:
@{
int number = 305;
number = number | 22;
}
A short version of this operation consists of using the compound bitwise disjunction made possible with this operator: |=. Here is an example:
@{
int number = 305;
number |= 22;
}
Bitwise Exclusion
Introduction
Bitwise exclusion consists of adding two bits with the following rules. To support bitwise exclusion, the C# language provides the ^ operator.
As an example, consider the number 1608 bit-excluded from 3175. The decimal number 1608 converted to binary is 11001001000. The decimal number 3175 converted to binary is 110001100111. Based on the above 2 points, we can bit-exclude these two numbers as follows:
Compound Bitwise Exclusion
You can bitwise-exclude one number from another and assign the value to a variable. Here is an example:
using System
int number1 = 2948;
int number2 = 9305;
int result = (number1 ^ number2);
}
You can bitwise exclude a number of bits from a variable and assign the result to another variable or to the variable itself. Here is an example:
@{
int number = 618;
number = number ^ 38;
}
As an alternative, you can use the compound bitwise exclusion operator represented as ^=. Here is an example:
@{
var number = 618;
number ^= 38;
}
Left-Shifting the Bits
Introduction
Left shifting the bits consists of pushing each bit from right to left. You can do this by one or more bits. To support this operation, the C# language provides an operator represented as <<.
Compound Left-Shifting or Bits
After shifting one or more bits of a variable, you can assign the result to a variable. Here is an example:
@{
int number = 248 << 5;
}
You can push a few bits to the left of a variable and assign the result to the variable itself. Here is an example:
@{
var number = 248;
number = number << 5;
}
To provide another version of this operation, the C# language is equipped with the compound left-shift operator represented by <<=. Here is an example of using it:
@{
var number = 248;
number <<= 5;
}
Right-Shifting the Bits
You can shift the bits to the right. Everything is done as reviewed for the left shift but in reverse order. To support this operation, the C# language provides the >> operator.
JavaScript Operators
The JavaScript language supports the same operators we have reviewed for C#.
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2001-2019, FunctionX | Next |
|