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
Control | (Name) | Text | Other Properties |
Label | Distribution Charges . . . . . . . . . . . . . . . | ||
TextBox | txtDistributionCharges | 0.00 | TextAlign: Right |
Label | Environment Charges . . . . . . . . . . . . . . | ||
TextBox | txtEnvironmentCharges | 0.00 | TextAlign: Right |
Button | btnCalculateTotalCharges | Calculate | |
Label | _______________________________ | ||
Label | Total Charges . . . . . . . . . . . . | ||
TextBox | txtTotalCharges | 0.00 | TextAlign: Right |
Introduction
We were already introduced ti the addition operation. It consists of adding one value to another. If the value is from a text box, you should convert the value before performing the operation.
Practical Learning: Using the Addition Operator
private void btnCalculateWaterAndSewerUsage_Click(object sender, EventArgs e)
{
double distributionCharges;
double environmentCharges;
double amountDue;
distributionCharges = double.Parse(txtDistributionCharges.Text);
environmentCharges = double.Parse(txtEnvironmentCharges.Text);
amountDue = distributionCharges + environmentCharges;
txtTotalCharges.Text = amountDue.ToString();
}
String Addition
We also saw that you can add two strings using the + operator. Here is an example:
class Exercise
{
static void Main()
{
string prefix;
string rest;
prefix = "De";
rest = "Niro";
string name = prefix + rest;
}
}
This would produce:
DeNiro
In the same way, you can add as many strings as you want the same would you would add many numbers. Here is an example:
class Exercise
{
static void Main()
{
string firstName;
string lastName;
string suffix;
string name;
firstName = "Samuel";
lastName = "Eto'o";
suffix = "Fils";
name = firstName + " " + lastName + ", " + suffix;;
}
}
This would produce:
Samuel Eto'o, Fils
Incrementing a Variable
We are used to counting numbers such as 1, 2, 3, 4, etc. In reality, when counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. The simplest technique of incrementing a value consists of adding 1 to it. You can apply this feature in computer programming. To do this, first declare a variable. Add 1 to that variable, and assign the result to the variable itself. This is illustrated in the following example:
// This program studies value incrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of incrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value = value + 1;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Techniques of incrementing a value Value = 12 Value = 13
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. The above program can be re-written as follows:
// This program studies value incrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of incrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value++;
System.Console.Write("Value = ");
System.Console.WriteLine(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++ is executed, the compiler takes the previous value of the variable, adds 1 to it, and the variable holds the incremented value:
// This program studies value incrementing class Exercise { static void Main() { int value = 12; System.Console.WriteLine("Techniques of incrementing a value"); value++; System.Console.Write("Value = "); System.Console.WriteLine(value); value++; System.Console.Write("Value = "); System.Console.WriteLine(value); Value++; System.Console.Write("Value = "); System.Console.WriteLine(value); } }
This would produce:
Techniques of incrementing a value Value = 13 Value = 14 Value = 15
Pre and Post-Increment
When using the ++ operator, the position of the operator with regard 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:
// This program studies value incrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of incrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
System.Console.Write("Value = ");
System.Console.WriteLine(++value);
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Techniques of incrementing a value Value = 12 Value = 13 Value = 13
When writing ++Value, the value of the variable is incremented before being called. On the other hand, if you want to first use a variable, then increment it, in other words, if you want to increment the variable after calling it, position the increment operator on the right side of the variable:
// This program studies value incrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of incrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
System.Console.Write("Value = ");
System.Console.WriteLine(value++);
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Techniques of incrementing a value Value = 12 Value = 12 Value = 13
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:
// This program studies value incrementing and decrementing
class Exercise
{
static void Main()
{
double value = 12.75;
double newValue;
System.Console.WriteLine("Techniques of incrementing and decrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
newValue = value + 2.42;
System.Console.Write("Value = ");
System.Console.WriteLine(newValue);
}
}
This would produce:
Techniques of incrementing and decrementing a value Value = 12.75 Value = 15.17
The above technique requires that you use an extra variable in your application. The advantage is that each value can hold its own value although the value of the second variable depends on whatever would happen to the original or source variable. Sometimes in your program you will not need to keep the original value of the source variable. You may want to permanently modify the value that a variable is holding. In this case, you can perform the addition operation 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:
// This program studies value incrementing and decrementing
class Exercise
{
static void Main()
{
double value = 12.75;
System.Console.WriteLine("Techniques of incrementing and decrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value += 2.42;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This program produces the same result as the previous. You can also perform a post-increment operation on variables. Here is an example:
private void Create() { double amountDue; amountDue = double.Parse(txtDistributionCharges.Text); amountDue += double.Parse(txtEnvironmentCharges.Text); txtTotalCharges.Text = amountDue.ToString(); }
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:
string name = "Jim"; 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:
class Exercise
{
static void Main()
{
double value1 = 224.58, value2 = 1548.26;
double result = value1 * value2;
}
}
The operatioh can involve two or more operand. 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
Control | (Name) | Text | Other Properties |
Label | Water and Sewer Usage . . . . . . . . . . . . | ||
TextBox | txtWaterSewerUsage | 0.00 | TextAlign: Right |
Label | Gallongs | ||
Button | btnCalculateWaterSewerUsage | Calculate Water and Sewer Usage | |
Label | Water Use Charges => 4.18 per 1,000 Gallons . . . . . . . . . . . . . . . | ||
TextBox | txtWaterUseCharges | 0.00 | TextAlign: Right |
Label | Water Use Charges => 5.85 per 1,000 Gallons . . . . . . . . . . . . . . . | ||
TextBox | txtWaterUseCharges | 0.00 | TextAlign: Right |
Label | Distribution Charges . . . . . . . . . . . . . . | ||
TextBox | txtDistributionCharges | 0.00 | TextAlign: Right |
Label | Environment Charges . . . . . . . . . . . . . . | ||
TextBox | txtEnvironmentCharges | 0.00 | TextAlign: Right |
Button | btnCalculateTotalCharges | Calculate Total Charges | |
Label | _______________________________ | ||
Label | Total Charges . . . . . . . . . . . . | ||
TextBox | txtTotalCharges | 0.00 | TextAlign: Right |
private void btnCalculateWaterAndSewerUsage_Click(object sender, EventArgs e) { double usage; double waterUseCharges; double sewerUseCharges; usage = double.Parse(txtWaterAndSewerUsage.Text); waterUseCharges = usage * 4.18; sewerUseCharges = usage * 5.85; txtWaterAndSewerUsage.Text = usage.ToString(); txtWaterUseCharges.Text = waterUseCharges.ToString("F"); txtSewerUseCharges.Text = sewerUseCharges.ToString("F"); } private void btnCalculateTotalCharges_Click(object sender, EventArgs e) { double waterUseCharges; double sewerUseCharges; double distributionCharges; double environmentCharges; double amountDue; waterUseCharges = double.Parse(txtWaterUseCharges.Text); sewerUseCharges = double.Parse(txtSewerUseCharges.Text); distributionCharges = double.Parse(txtDistributionCharges.Text); environmentCharges = double.Parse(txtEnvironmentCharges.Text); amountDue = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges; txtTotalCharges.Text = amountDue.ToString("F"); }
Compound Multiplication
You can multiply a value by a variable and assign the result to the same variable. Here is an example:
class Exercise
{
static void Main()
{
double value = 12.75;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value = value * 2.42;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Value = 12.75 Value = 30.855 Press any key to continue . . .
To make this operation easy, the C# language supports the compound multiplication assignment operator represented as *=. To use it, use the *= operator and assign the desired value to the variable. Here is an example:
class Exercise
{
static void Main()
{
double value = 12.75;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value *= 2.42;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
Introduction
The subtraction is performed with the - symbol. Here is an example:
class Exercise
{
static void Main()
{
// Values used in this program
double value1 = 224.58, value2 = 1548.26;
double 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
Control | (Name) | Text |
Label | Meter Reading Start . . . . . . . . . . . . | |
TextBox | txtMeterReadingStarStart | |
Label | Gallons | |
Label | Meter Reading End . . . . . . . . . . . . | |
TextBox | txtMeterReadingStarEnd | |
Label | Gallons | |
Button | btnCalculateWaterAndSewerUsage | Calculate Water and Sewer Usage |
Label | _______________________________ | |
Label | Water and Sewer Usage . . . . . . . . . . . . | |
TextBox | txtWaterSewerUsage | |
Label | Gallons | |
Label | Water Use Charges => 4.18 per 1,000 Gallons . . . . . . . . . . . . . . . . . . . . . . | |
TextBox | txtSewerUseCharges | |
Label | Sewer Use Charges => 5.85 per 1,000 Gallons . . . . . . . . . . . . . . . . . . . . . | |
TextBox | txtSewerUseCharges | |
Label | Distribution Charges . . . . . . . . . . . . . . | |
TextBox | txtDistributionCharges | |
Label | Environment Charges . . . . . . . . . . . . . . | |
TextBox | txtEnvironmentCharges | |
Button | btnCalculateTotalCharges | Calculate Total Charges |
Label | _______________________________ | |
Label | Total Charges . . . . . . . . . . . . | |
TextBox | txtTotalCharges |
private void btnCalculateWaterAndSewerUsage_Click(object sender, EventArgs e) { double meterReadingStart; double meterReadingEnd; double usage; double waterUseCharges; double sewerUseCharges; meterReadingStart = double.Parse(txtMeterReadingStart.Text); meterReadingEnd = double.Parse(txtMeterReadingEnd.Text); usage = meterReadingEnd - meterReadingStart; waterUseCharges = usage * 4.18; sewerUseCharges = usage * 5.85; txtWaterSewerUsage.Text = usage.ToString(); txtWaterUseCharges.Text = waterUseCharges.ToString("F"); txtSewerUseCharges.Text = sewerUseCharges.ToString("F"); } private void btnCalculateTotalCharges_Click(object sender, EventArgs e) { double waterUseCharges; double sewerUseCharges; double distributionCharges; double environmentCharges; double amountDue; waterUseCharges = double.Parse(txtWaterUseCharges.Text); sewerUseCharges = double.Parse(txtSewerUseCharges.Text); distributionCharges = double.Parse(txtDistributionCharges.Text); environmentCharges = double.Parse(txtEnvironmentCharges.Text); amountDue = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges; txtTotalCharges.Text = amountDue.ToString("F"); }
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:
// This program studies value decrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of decrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value = value - 1;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Techniques of decrementing a value Value = 12 Value = 11
As done to increment, C# provides a quicker way of subtracting 1 from a value. This is done using the decrement operator, that is --. To use the decrement operator, type - on the left or the right side of the variable when this operation is desired. Using the decrement operator, the above program could be written:
// This program studies value decrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of decrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value--;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
Pre-Decrementing a Value
Once again, 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. This is illustrated in the following program:
// This program studies value decrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of decrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
System.Console.Write("Value = ");
System.Console.WriteLine(--value);
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Techniques of decrementing a value Value = 12 Value = 11 Value = 11
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:
// This program studies value decrementing
class Exercise
{
static void Main()
{
int value = 12;
System.Console.WriteLine("Techniques of decrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
System.Console.Write("Value = ");
System.Console.WriteLine(Value--);
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Techniques of decrementing a value Value = 12 Value = 12 Value = 11
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:
// This program studies value incrementing and decrementing
class Exercise
{
static void Main()
{
double value = 12.75;
System.Console.WriteLine("Techniques of incrementing and decrementing a value");
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value -= 2.42;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Techniques of incrementing and decrementing a value Value = 12.75 Value = 10.33
Introduction
The division is used to get the fraction of one number in terms of another. The division is performed with 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). It is also a good idea to make sure that you know the relationship(s) between the numbers involved in the operation.
Practical Learning: Using the Division Operator
uprivate void btnCalculateWaterAndSewerUsage_Click(object sender, EventArgs e) { double meterReadingStart; double meterReadingEnd; double usage; double waterUseCharges; double sewerUseCharges; double waterConsumption; double sewerServices; meterReadingStart = double.Parse(txtMeterReadingStart.Text); meterReadingEnd = double.Parse(txtMeterReadingEnd.Text); usage = meterReadingEnd - meterReadingStart; waterUseCharges = usage * 4.18; sewerUseCharges = usage * 5.85; waterConsumption = waterUseCharges / 1000; sewerServices = sewerUseCharges / 1000; txtWaterSewerUsage.Text = usage.ToString(); txtWaterUseCharges.Text = waterConsumption.ToString("F"); txtSewerUseCharges.Text = sewerServices.ToString("F"); } private void btnCalculateTotalCharges_Click(object sender, EventArgs e) { double waterUseCharges; double sewerUseCharges; double distributionCharges; double environmentCharges; double amountDue; waterUseCharges = double.Parse(txtWaterUseCharges.Text); sewerUseCharges = double.Parse(txtSewerUseCharges.Text); distributionCharges = double.Parse(txtDistributionCharges.Text); environmentCharges = double.Parse(txtEnvironmentCharges.Text); amountDue = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges; txtTotalCharges.Text = amountDue.ToString("F"); }
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:
class Exercise
{
static void Main()
{
double value = 12.75;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value = value / 2.42;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
This would produce:
Value = 12.75 Value = 5.26859504132231 Press any key to continue . . .
To perform this operation faster, the C# language provides the /= operator. Here is an example of using it:
class Exercise
{
static void Main()
{
double value = 12.75;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
value /= 2.42;
System.Console.Write("Value = ");
System.Console.WriteLine(value);
}
}
Introduction
The division program gives a result of a number with decimal values if you provide an odd number (like 147), which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. Imagine you have 26 kids at a football (soccer) stadium and they are about to start. You know that you need 11 kids for each team to start. If the game starts with the right amount of players, how many will seat and wait?
The remainder operation is performed with the percent sign (%) which is gotten from pressing Shift + 5.
Here is an example:
class Exercise
{
static void Main()
{
int players = 18;
int remainder = players % 11;
// When the game starts, how many players will wait?.
System.Console.Write("Out of ");
System.Console.Write(players);
System.Console.Write(" players, ");
System.Console.Write(remainder);
System.Console.WriteLine(" players will have to wait when the game starts.\n");
}
}
This would produce:
Out of 18 players, 7 players will have to wait when the game starts. Press any key to continue . . .
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:
class Exercise
{
static void Main()
{
int players = 18;
// When the game starts, how many players will wait?.
System.Console.Write("Out of ");
System.Console.Write(players);
System.Console.Write(" players, ");
players = players % 11;
System.Console.Write(players);
System.Console.WriteLine(" players will have to wait when the game starts.\n");
}
}
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:
class Exercise
{
static void Main()
{
int Players = 18;
// When the game starts, how many players will wait?.
System.Console.Write("Out of ");
System.Console.Write(players);
System.Console.Write(" players, ");
players %= 11;
System.Console.Write(players);
System.Console.WriteLine(" players will have to wait when the game starts.\n");
}
}
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2001-2021, C# Key | Next |
|