Operators In C#

C# offers a wide set of operators for you to write any kind of expression. Expressions are same as you have seen in your Maths class. For e.g.  a = b + c. Here a, b and c are operands whereas = and + are operators.



These operators can be divided into following categories -

 Type
 Operators
 Arithmetic  +  -  *  /  %  ++  --  
 Relational ==  !=  >  < >=  <=
 Logical &&  ||  !  &  |
 Other =  ?:  ??

Arithmetic Operators


These operators + (Addition), - (Subtraction), * (Multiplication), / (Division) works as expected. Other operators in this category are easy to understand. % (Modulus Operator) - This operator is used to get the remainder when you divide two numbers. 

Let us take one e.g. to make it more clear. When you divide 13 by 2, you get 6 as quotient and 1 as remainder because 2 * 6 + 1 = 13. To get this remainder value in a variable use 13%2, this will return 1 as output. There are various programming problems in Computer Science that requires evaluation of remainder.

NOTE: When you divide 2 integers, output will be an integer as per C# specification. To get result as floating point, you need to use casting. (Casting will be explained in other articles.)

using System;

class Program
{
    static void Main(String[] args)
    {
        int i = 13;
        int j = 2;
        float resultDivideFloat = (float)i / j; //PRINTS 6.5 CASTING AS FLOAT
        int resultDivideInteger = i / j; // PRINTS 6 BECAUSE BOTH ARE INTEGERS
        int resultModulusInteger = i % j; // REMAINDER IS 1
        Console.WriteLine("i/j stored in float variable = " + resultDivideFloat);
        Console.WriteLine("i/j stored in integer variable = " + resultDivideInteger);
        Console.WriteLine("i%j Modulus Operator= " + resultModulusInteger);

        Console.Read();
    }
}

cheezycode-csharp-modulus-operator


Increment Operator and Decrement Operator - These operators are used to increment or decrement the value of a variable by 1 in memory itself. For example - when you use x++, it means x = x + 1. Same applies to decrement operator i.e. x-- means x = x - 1. There are 2 variants of this operator Postfix and Prefix which are based on 

First use the value then increment - Postfix (y = x++)
First increment the value then use it - Prefix (y = ++x)


Relational Operators


These operators are used to compare two values and returns either true or false. So if you want to execute logic like if A is greater than B do this or A is equal to B or A is smaller than B then you need to use these operators.


using System;

class Program
{
    static void Main(String[] args)
    {
        int a = 13;
        int b = 2;
        Console.WriteLine("a = {0} and b = {1} \n", a, b);
        Console.WriteLine("Is a > b ? " + (a > b));
        Console.WriteLine("Is a < b ? " + (a < b));
        Console.WriteLine("Is a >= b ? " + (a >= b));
        Console.WriteLine("Is a <= b ? " + (a <= b));
        Console.WriteLine("Is a == b ? " + (a == b));
        Console.WriteLine("Is a != b ? " + (a != b));
        Console.Read();
    }
}

cheezycode-csharp-relational-operator

Logical Operators


These operators are used to combine various conditions together in a logical way. AND(&&), OR(||) and NOT(!) are logical operators. For scenarios, where you have conditions like if number entered by user is greater than 10 and less than 100 - you require logical operators.

using System;

class Program
{
    static void Main(String[] args)
    {
        int number = 0;
        Console.WriteLine("Enter number between 10 and 100 : ");
        string enteredNumber = Console.ReadLine();
        int.TryParse(enteredNumber, out number);
        Console.WriteLine("Have you entered number between 10 and 100 ?  " + ( number > 10 && number < 100 ));
        Console.Read();
    }
}

cheezycode-csharp-logical-operator

Assignment Vs Equality Operator


In C#, assignment of value to a variable is done by assignment operator. When you say int x = 10; this means you are initializing a variable with a value 10. Value from right hand side is assigned to left hand side. This is different from Maths(=) operator which is used to denote equality. Equality in C# denoted by double equal i.e. (==). So to check whether two values are equal use == . For e.g. a==b is used to check whether value of a is equal to value of b.

== is Equality Operator and  = is assignment operator

Other operators are explained where necessary. These are more than enough to get started. Let us know if you have any query or concerns. Happy Reading (refer full series here - C# Tutorial Index )




Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example