c sharp

Subtraction Assignment Operator (-=) in C#

In C# programming, we solve millions of problems using operators. Operators perform different operations and are also used for assigning values to variables. The assignment operators give value to the variables. We have what we call the compound assignment operators in which the subtraction assignment operator is one of them. There are eleven types of compound assignment operators. All of them perform an operation and assign the value to the variable. The equal operator (=) is utilized to assign the value to any variable despite its data type. It is identified as assignment operator. The value on which we perform different operations is called an operand. In subtraction assignment, the right value is assigned to the left operand. It subtracts the value from the variable and the resultant value is assigned to that variable. For example, P – = 9 works as P = P-9. We can say that subtraction assignment is a shorthand method for subtracting and assigning the values to a variable like a=a-value.

Syntax:

Operand -= Operand

The operands can be any variable or value that subtracts the second operand from the first and stores the result in the first operand.

Example 1:

We will use the subtraction assignment operator with an integer value and will see the difference before and after assigning this operator.

First, declare and initialize the “value_1” variable of integer type. Then, print a message by calling the Console.WriteLine() function. The integer that is stored in the “value_1” variable is also shown using the Console.WriteLine() method. Here, we utilize the Console.WriteLine() method by concatenating (+) the message with the variable name, “value_1”. In the next statement, the “7” digit is subtracted from the “value_1” using the subtraction assignment operator. Then, utilize a Console.WriteLine() function to display a text along with its value. This function prints the new value of “value_1”. Since C# is compiled line by line, as 78 is the initial value assigned to the “value 1” variable, the compiler will display it first. The compiler will then perform the subtraction assignment operation and will display the new value when it compiles the second console method.

Example 2:

We will observe how to use the subtraction assignment operator in the “if else” statement when we are dealing with two integer values and when both operands are variables.

Here, we define two integer type variables – “val_1” and “val_2”. Then, use the “if-else” statement to check the required condition. The condition is that if the first value which is “val_1” is greater than 70, we execute the body. We utilize the subtraction assignment operator in the body of the “if” statement to subtract the second value which is “val_2” from the first value “val_1”. Then, we assign the value to “val_1”. Then, it shows the result along with the message on the console. If the defined condition of the “if” expression is not fulfilled, we execute the “else” part. We use the subtraction assignment operator in the “else” part to subtract the first value “val_1” from the second value “val_2”. Then, we assign the value to the “val_2”. Then, present the value on the screen by employing the Console.WriteLine() function.

The body of the “if” statement is evaluated in this code because the expression of the “if” statement is satisfied.

Example 3:

This example will cover the usage of the subtraction assignment operator in the “for” loop. It keeps iterating until the given condition is met.

An integer variable “number_1” is declared and assigned a value of “80”. Next, use a “for” loop. Define and initialize the iterator inside this loop. Then, set the condition for iteration and increment the value of “i”. In the body of the “for” loop, do the subtraction assignment operation of the “number_1” variable by subtracting it from 2. Then, display the values one after another with the use of the “for” loop. The “for” loop goes through four times and displays the value every time it executes the body. The Console.WriteLine() function is used within the “for” loop to concatenate the value of “i” with the message. The value updates each time when the loop is executed. By doing this, we will see the updated value on the console. Whenever the “for” loop is executed, it increases the value of the “i” iterator. When the “i” is greater than the required condition, the loop will terminate.  The loop starts from 0 and it prints the original value subtracted by 2 and so on.

Example 4:

Now, let’s use the subtraction assignment operator with the floating point values.

In the first statement, two floating point values are initialized. Then, a “for” loop is applied. Within the “for” loop, initialize the iterator with a “0” value and define the condition. This loop executes 6 times because the condition is defined that the iterator is less than and equal to five. The “i++” expression increments the iterator until it makes the given condition, false. The iterator starts from 0 to 5 which executes the “for” loop 6 times every time the value of “no_1” is subtracted by the value of “no_2”. When the value is subtracted for the first time, it updates the value of the “no_1” variable and the process continues until the condition of the “for” loop is true. However, the loop ends and the remaining code runs when the value of “i” equals to 6. Outside the “for” loop, we have one statement that shows the last value which is stored in the “no_1” variable.

Conclusion

The usage of the subtraction assignment (-=) operator in the codes of C# language is thoroughly explained in this guide. With the help of practical code examples, we explored everything regarding the subtraction assignment (-=) operator. This operator can be used as a shorthand method to subtract something from the variable and for assigning the resultant value to that variable. The assignment operator can perform subtraction and assignment of two variables.

About the author

Ann-ul Hayyat

I'm a professional SEO and technical content writer with extensive experience in these fields as well as writing content for blogs, websites, reports, and other topics related to education, science, sports, and many other areas. I am skilled and experienced in creating SEO-friendly blog posts, technical articles, and other content on themes that you may find interesting.