An operator is a symbol that is used to perform certain Mathematical and logical Manipulations . An operator is basically used in programs to manipulate data and variables.
There are some types of of operators that supports C# language.
Arithmetic operators are used as shown below:There are some types of of operators that supports C# language.
x - y
x + y
x * y
x / y
x % y
Note:- Here x and y are known as operands.
There are three types of arithmetic operators which is shown below:
1.1 ) Integer Arithmetic
1.2 ) Real Arithmetic
1.3 ) Mixed mode Arithmetic
1.1 ) Integer Arithmetic:
In integer Arithmetic ,if both the operands in a single expression such as( x + y ) are integers, the expression is called an integer expression and the operation is called integer arithmetic.
Ex.
Let x =10 and y = 5 then result will be following.
x +y = 15
x - y = 5
x * y = 50
x / y = 2
x % y = 0
1.2 ) Real Arithmetic:
A Real Arithmetic operation involving only real (floating point)operators is called Real arithmetic.
Ex.
Let x = 10.40 and y = 3.60
then:
x + y = 14.00
x - y = 6.80
x * y = 37.44
x / y = 2.97
x % y = 3.2
1.3 ) Mixed mode Arithmetic:
When one operand is integer and the other operands is Real Number then the expression is known as mixed mode arithmetic expression.
Ex.
if x = 20 ,y = 1.5
x + y = 21.5
x - y = 21. 5
x * y = 30.0
x / y = 13
C# Program on Arithmetic Operators:-
using System;
namespace operators
{
class Program
{
static void Main(string[] args)
{
int x = 20, y = 10;
Double z = 20.50;
Console.WriteLine("x+y=" + (x + y));
Console.WriteLine("x-y=" + (x - y));
Console.WriteLine("x*y=" + (x * y));
Console.WriteLine("x/y=" + (x / y));
Console.WriteLine("x%y=" + (x % y));
Console.WriteLine("x+z=" + (x + z));
Console.WriteLine("x*z=" + (x * z));
Console.WriteLine("z/x=" + (z / x));
Console.WriteLine("z/y=" + (z / y));
Console.ReadLine();
}
}
}
2. ) Logical operator: The Logical operators are used when we want to form compound conditions by combining two or more Operators(relations).
Ex.
If (x>50 && y<10)
{
i++;
}
Note:-If both x and y value are true according to the expression the i value will be incremented.
There are some Logical operators which are shown below table:
Truth table of Logical Operators:-
C# program on Logical Operators:-
using System;
namespace logical
{
class Program
{
static void Main(string[] args)
{
int x = 60, y = 500;
int z = 1000;
if (x > 30 && y < 1000 || z >= 1000)
{ x++;
y--;
z--;
x = x-- + ++z;
Console.WriteLine("value of x is :"+x);
Console.WriteLine("value of y is :" + y);
Console.WriteLine("value of z is :" + z);
Console.ReadLine();
}
}
}
}
Output:3. ) Relational operators:
When we want to compare two or more quantities then we use relational operators.Relational operators are more useful to establish the relation between two or more operands.
Ex.
Let an expression is:
x <= 20 or x > 30
If x = 10 then the expression:
x <= 20 is true.
x > 30 is false.
If x =25 then the expression:
x <= 20 is false.
x > 30 is false.
Note:- The value of Relational Expression is either true or false.Simple relational expression
contains only one operators but complex relation expression contains more than one operators.
There are some relational operators which are shown below table:
C# Program on Relational operators:-
Output:-
4. ) Assignment operators:-
An assignment operators are used to assign the value of an Expression to a variables (operands).
The operators '=', += ,-= are known as assignment operators.
Ex.
Let x=5, y= 20
using System;
namespace Relational
{
class Program
{
static void Main(string[] args)
{
int x = 40, y = 80;
float z = 15.0F;
Console.WriteLine("x<y is" + (x < y));
Console.WriteLine("x>y is" + (x > y));
Console.WriteLine("x<=y is" + (x <= y));
Console.WriteLine("x!=y is" + (x != y));
Console.WriteLine("x>=y is" + (x >= y));
Console.WriteLine("x==y is" + (x == y));
Console.WriteLine("z is" + z);
Console.ReadLine();
}
}
}
Output:-
4. ) Assignment operators:-
An assignment operators are used to assign the value of an Expression to a variables (operands).
The operators '=', += ,-= are known as assignment operators.
Ex.
x += y <=> x = x + y;
output
x = 25 <=> x= 25
The use of Assignment operators has some advantages.
- The statement is more concise and easier to read.
- It is more easier to write.
- The use of shorthand operators result is a more efficient code.
Shorthand assignment operators are:
C# program on Assignment operators:
using System;
namespace assignment
{
class Program
{
static void Main(string[] args)
{
int x = 10, y = 20;
x += y;
y += x;
x -= y;
y -= x;
Console.WriteLine("value of x is:" + x);
Console.WriteLine("value of y is:" + y);
Console.ReadLine();
}
}
}
5. ) Conditional Operators:-
The character pair ? : is a ternary operator in C#. This operator is used to make conditional Expression in C#.
Ex. 1
exp1 ? exp2 : exp3
Where exp1 ,exp2 and exp3 are expressions.
The operator ? : works as follow:
- exp1 is evaluated first, if it is true, then exp2 is evaluated and becomes the value of the conditional expression.
- If exp1 is false then exp3 is evaluated and becomes the value of the expression.
Ex. 2
Let x=20 and y=30
and
Expression z =x > y ? x : y ;
Output: Z = 30
Description : x>y is false so expression third ( y) is evaluated and becomes the value of this conditional expression.
6 . ) Bitwise operators: The Bitwise operators are used to manipulation of data at bit level.These are basically used for testing the bits or shifting the bit to right or left. Floating point Number are not supported Bitwise Operators.
C# Program on Bitwise operators:
using System;
namespace bitwise
{
class Program
{
static void Main(string[] args)
{
byte bAnd, bOR, bXOR;
int bwright, bwleft, bwcomp;
bAnd = 13 & 12;
bOR = 17 | 3;
bXOR = 10 ^ 4;
bwright = 20 >> 5;
bwleft = 50 << 6;
bwcomp = ~ 65;
Console.WriteLine("bAnd result is:" + bAnd);
Console.WriteLine("bOR result is:" + bOR);
Console.WriteLine("bXOR result is:" + bXOR);
Console.WriteLine("bwright result is:" + bwright);
Console.WriteLine("bwleft result is:" + bwleft);
Console.WriteLine("bwcomp result is:" + bwcomp);
Console.ReadLine();
}
}
}
Output:
7. ) Increment and Decrement operators:
The operators ++ and -- is known as Increment and Decrement operators in C# or other language.
++x --> Preincrement
x++ --> PostIncrement
--x --> PreDecrement
x-- --> PostDecrement
- Preincrement(++x): Whenever you find this statement(++x) in the program ,then after just this statement in the program value of x will be incremented immediately by one.
- PostIncrement(x++): After just this statement(x++) in the program value of x will not be incremented immediately by one.
- PreDecrement(--x): After just this statement (--x) value of x will be decremented immediately b y one.
- PostDecrement(x--): After just this statement(x--) value of x will not be Decremented immediately by one.
Ex.
using System;
namespace increment_and_Decrement
{
class Program
{
static void Main(string[] args)
{
int x=10,y=20;
x = ++x;
Console.WriteLine("value of x is:" + x);
x = x++;
Console.WriteLine("value of x is:" + x);
x = --x;
Console.WriteLine("value of x is:" + x);
x = x--;
Console.WriteLine("value of x is:" + x);
y = ++x + x++ + --x + x--;
Console.WriteLine("value of y is:" + y);
Console.ReadLine();
}
}
}
output:
8. ) Special operators:-
There are some special operators that is used in c# which are given below:
is --> Relational operator
typeof --> type operator
as --> Relational operator
sizeof --> size operator
new --> object operator
checked --> overflow checking
unchecked --> prevention of overflow checking
.(dot) --> member access operator
Operator precedence and Associativity in C#:-
This precedence is basically used to determine how one operator is evaluated from an expression.The operators that have higher level of precedence are evaluated first.When two operators have same precedence then they are evaluated either from left to right or right to left depending on the level .This is known as Associativity of an operator.
For More:-
To Get the Latest Free Updates Subscribe
Click below for download whole program
Download
0 comments:
Post a Comment