Team LiB
Previous Section Next Section

Arithmetic Operators

C#, like any other business language, offers a broad range of mathematical operators. Table 2-1 lists the C# arithmetic operators.

Table 2-1: C# Arithmetic Operators

Operator

Description

Subtraction or unary minus

+

Addition or unary plus

*

Multiplication

/

Division

%

Modulus division (remainder)

=

Subtraction assignment

+=

Addition assignment

*=

Multiplication assignment

/=

Division assignment

%=

Modulus division assignment

−−

Decrement

++

Increment

The following example shows the use of arithmetic operators in C#.

Code Example: Arithmetic Operators
Start example

using System;

namespace Client.Chapter_2___ Expressions_and_Operators
{
      class MyMainClass
      {
            static void Main(string[] args)
              {
                  int a,b,c,d,e,f;

                  a = 1;
                  //Addition
                  b = a + 6;
                  //Subtraction
                  c = b - 3;
                  //Multiplication
                  d = c * 2;
                  //Division
                  e = d / 2;
                  //Modulus
                  f = e % 2;
              }
      }
}
End example

Multiple-Function Operators (+=, =, *=, /=, and %=)

In addition to the normal arithmetic operators, C# provides several multiple-function operators. These operators include the following:

  • Addition assignment (+=)

  • Subtraction assignment (=)

  • Multiplication assignment (*=)

  • Division assignment (/=)

  • Modulus assignment (%=)

These multiple-function assignment operators instruct the compiler to add the value of the variable on the left side of the operator to the value of the expression to the right, and then assign the results of the computation to the variable on the left.

Unary Operators (++ and −−)

The increment (++) and decrement (−−) operators are unary operators that act on a single operand. If the ++ or −− operator is placed before the operand, the value of the operand will be incremented or decremented before the rest of the expression is evaluated. If this operator is placed after the operand, the value of the operand is assigned first, and then incremented or decremented after the assignment.

Caution 

Be careful where you place a unary arithmetic operator. The position of the ++ or −− operator can affect the results of the operation.

The following is an example of using the multiple-function and unary operators.

Code Example: Multiple-Function and Unary Operators
Start example

using System;

namespace Client.Chapter_2___ Expressions_and_Operators
{
      class NumericOperators1
      {
            static void Main(string[] args)
            {
                  int a, b, c, d, e;

                  a = 1;
                  //a = a + 1
                  a += 1;
                  b = a;
                  //b = b - 2
                  b -= 2;
                  c = b;
                  //c = c * 3
                  c *= 3;
                  d = 4;
                  //d = d / 2
                  d /= 2;
                  e = 23;
                  //e = e % 3
                  e %= 3;
            }
      }
}

using System;

namespace Client.Chapter_2___Operators_and_Expressions
{
      class NumericOperators2
      {
            static void Main(string[] args)
            {
                    int a,b,c,d,e,f;
                  a = 1;
                  b = a + 1;       //b = 2
                  b = b - 1;       //b = 1
                  c = 1; d = 2;
                  ++c;             //c = 2
                  --d;             //d = 1
                  e = --c;         //e = 1 c =1
                  f = c--;         //f = 1 c= 0
          }
    }
}
End example

Team LiB
Previous Section Next Section