Friday, 17 August 2018

C#: Compound Assignment Operators

Compound Assignment
      Syntax:
         variable operator= value

      Example:
         int a = 10;
         a+=10 evaluates to a=a+10

Compound Operator
Example
Description
+=
a+=10
a=a+10
-=
a-=10
a=a-10
*=
a*=10
a=a*10
/=
a/=10
a=a/10

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int var1 = 100;
        int sum = 10, sub = 10, mul = 10, div = 1000;

        Console.WriteLine("sum+var1 is " + (sum += var1));
        Console.WriteLine("sub-var1 is " + (sub -= var1));
        Console.WriteLine("mul*var1 is " + (mul *= var1));
        Console.WriteLine("div/var1 is " + (div /= var1));
    }
}

Output
sum+var1 is 110
sub-var1 is -90
mul*var1 is 1000
div/var1 is10



Previous                                                 Next                                                 Home

No comments:

Post a Comment