Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, October 1, 2016

Lesson 5 | Operators in C#

C# provides a large set of operators, which are symbols that specify which operations to perform in an expression.

It  is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Below are different types of operators in C#.

Assignment operator
=
Additive
Multiplicative
Relational and type testing
Equality
Conditional AND
Conditional OR
Conditional
Null-coalescing

1)Let us start with assignment operator
If we have variable I and we want to assign value 10 to this, we can use assignment operator
int i = 10;
if we want to assign true or false value to a bool type variable,we can use assignment operator.
bool check = true;

2) Additive
If we want to add 2 numbers, we will use Additive operator  +
int i = 10;
int j = 20;
int result=i+j;
Console.WriteLine("result is {0} ",result);
Console.ReadLine();

Please visit Operators in C# video tutorials.


 3)Multiplicative

If we divide 2 numbers,we get quotient and remainder.
if we use /,we get quotient.
int Numerator = 20;
int Denominator = 5;
int result = Numerator / Denominator;
Console.WriteLine("result is {0} ",result);
Console.ReadLine();

result=4

if we use %,we get remainder.
int Numerator = 21;
int Denominator = 5;
int result = Numerator % Denominator;
Console.WriteLine("result is {0} ",result);
Console.ReadLine();

result=1

4) Relational
If we want to check 2 numbers which is greater or smaller, we have to use Relational  operator
int x = 50;
int y = 20;

if (x > y)
{
Console.WriteLine("x is greater than y");
}
Console.ReadLine();

5) Equality
If we want to compare 2 numbers, we have to use Equality operator
int x = 20;
int y = 20;

if (x == y)
{
Console.WriteLine("x is equal to y");
}
Console.ReadLine();

if we want to check that x is not equal to y,we have to use !=
int x = 20;
int y = 25;

if (x != y)
{
Console.WriteLine("x is not equal to y");
}
Console.ReadLine();


6) Conditional AND  OR
If we have to check 2 conditions we can use && operator
int x = 50;
int y = 20;
if (x == 50 && y==20)
 {
 Console.WriteLine("both conditions are true");
 }
 Console.ReadLine();

If we want to satisfy one condition between two , we use or operator
int x = 50;
int y = 21;
if (x == 50 || y==20)
{
Console.WriteLine("one of two condition is true ");
}
Console.ReadLine();

No comments :

Post a Comment