By 22/07/2015 11 Comments

C# Arithmetic Operators

  • Arithmetic operators are used to perform basic or simple operations on values / variables / built-in data types.
  • Arithmetic operators are performed actions on more than one value or variable.

Suppose:

  int v1 = 5, v2 = 5;

OPERATOR DESCRIPTION
+ Plus, this operator performs the addition action on two variables or operands. Add v1 with v2, e.g. v1 + v2 = 10
Minus, this operator performs the subtraction action on two variables or operands. Subtracts v1 from v2, e.g. v1 – v2 = 0
* Multiplication, this operator performs the multiplication action on two variables or operands. Multiply v1 with v2, e.g. v1 * v2 = 25
/ Division, this operator performs the division action on two variables or operands. Divide v1 with v2, e.g. v1 / v2 = 1
% Modulus/Reminder, this operator performs the reminder action of division on two variables or operands. Modulus v1 with v2, e.g. v1 % v2 = 0. Notice! When we divide v1 with v2 we get a reminder, that reminder is declared via Modulus operator.
++ Increment, this operator increases or adds 1 to a variable or an operand, e.g. ++v1, now v1 has 6 value, Notice! If we want to add only 1 value to our variable or operand we should use ++ increment operator because it adds only 1 to our current value every time.
– – Decrement, this operator decreases or subtracts 1 from a variable or an operand, e.g. –v1, now v1 has 4 values, Notice! If we want to subtract only 1 value from our variable or operand we should use – – decrement operator because it subtracts only 1 from our current value every time.

Example:

using System;
namespace csharpBasic
{
  // Start class definition / declaration.
    class Program
    {
      // Static main method void type declaration.
        static void Main(string[] args)
        {
          // Declare and initialize v1 and v2 int type variables with 5,5 values.
            int v1 = 5, v2 = 5;

          // Declare an int type variable result.
            int result;

          // Perform Addition between v1 and v2, assign result to (result) variable.
            result = v1 + v2;

          // Print (result) variable which is 10
            Console.WriteLine("Plus: {0} ", result);

          // Perform Substraction between v1 and v2, assign result to (result) variable.
            result = v1 - v2;

          // Print (result) variable which is 0.                           
            Console.WriteLine("Minus: {0} ", result);

          // Perform Multiplication between v1 and v2, assign result to (result) variable.
            result = v1 * v2;

          // Print (result) variable which is 25.
            Console.WriteLine("Multiplication: {0} ", result);

          // Perform Devision between v1 and v2, assign result to (result) variable.
            result = v1 / v2;

          // Print (result) variable which is 1.
            Console.WriteLine("Division: {0} ", result);

          // Perform Modulus/Reminder between v1 and v2, assign result to (result) variable.
            result = v1 % v2;

          // Print (result) variable which is 0.
            Console.WriteLine("Modulus/Reminder: {0} ", result);

          // Perform increment operation and display the result.
            Console.WriteLine("Increment: {0} ", ++v1);

           // Perform decrement operation and display the result.
            Console.WriteLine("Decrement: {0} ", --v1);

            Console.ReadKey();

        } // End of main method definition.

    } // End of class.
}

Note:

There are two forms of Increment (++) and Decrement (- -) operators.

  • Pre-increment (++x) / pre-decrement (- -x)
  • Post- increment (x++) / post-decrement (x- -)

Example:

using System;
namespace csharpBasic
{
  // Start class definition / declaration.
    class Program
    {
      // Static main method void type declaration.
        static void Main(string[] args)
        {
           // Initialize and declare x variable of an int type with value of 10.
            int x = 10;

           // Declare an int type variable y.
            int y;

          /* In this expression the pre-increment (++x) form is used. In this 
             case x variable is first incremented and 
             then its value is assigned / returned to variable y. 
             Now the value of y variable is 11. */
            y = ++x;

           // Print variable y which is 11
            Console.WriteLine("pre-increment :{0} ", y);

           // Re-initialize x variable of an int type with value of 10.
            x = 10;

           /* In this expression the post-increment(x++) form is used. In this case the value of x 
              Variable is first obtained, x is incremented and then the original value of x
              (Which is 10) is assigned / returned to variable y. Now the value of y variable is 10,
              This value remains same because the value of x is returned first and then performed 
              Post-increment operation. */
            y = x++;

           // Print variable y which is 10.
            Console.WriteLine("post-increment: {0} ", y);

           // Re-initialize x variable of an int type with value of 10.
            x = 10;

           /* in this expression the pre-decrement (--x) form is used. In this case x variable is first 
              decremented and then its value is assigned / returned to variable y. Now 
              the value of y variable is 9. */
            y = --x;

           // Print variable y which is 9.
            Console.WriteLine("pre-decrement :{0} ", y);

           // Re-initialize x variable of an int type with value of 10.
            x = 10;

           /* in this expression the post-decrement(x--) form is used.  In this case the value of x 
              variable is first obtained, x is decremented and then the original value of 
              x (which is 10) is assigned / returned to variable y. Now the value of y variable is 10,
              this value remains same because the value of x is returned first and then performed
              post-decrement operation. */
            y = x--;

           // Print variable y which is 10.
            Console.WriteLine("post-decrement: {0} ", y);
            Console.ReadKey();

        } // End of main method definition.

    } // End of class.
}







← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .