operators in C#
Operators:
v Operators
are used to perform operations on variables and values.
v An
operator is a symbol that tells the compiler to perform specific mathematical
or logical manipulations.
v Operators
are the foundation of any programming language. Thus the functionality of C#
language is incomplete without the use of operators.
C# has rich set of built-in operators
and provides the following type of operators:
1. Arithmetic Operators.
2. Relational Operators.
3. Logical Operators.
4. Assignment Operators.
5. Conditional or Ternary Operator.
1. Arithmetic Operators:
These
are used to perform arithmetic/mathematical operations on operands.
Following table shows all the arithmetic
operators supported by C#. Assume variable A holds 20 and variable B holds 10,
then –
|
Operator |
Description |
Example |
|
+ |
Adds two operands |
A + B = 30 |
|
- |
Subtracts second operand from the first |
A - B = 10 |
|
* |
Multiplies both operands |
A * B = 200 |
|
/ |
Divides numerator by de-numerator |
A / B = 2 |
|
% |
Modulus Operator and remainder of after an
integer division |
A % B = 0 |
|
++ |
Increment operator increases integer value by one |
A++ (A+1) = 11 |
|
-- |
Decrement operator decreases integer value by one |
A-- (A-1)
= 9 |
2. Relational Operators:
Relational operators are used for
comparison of two values.
The return value of a comparison is
either true or false.
|
Operator |
Description |
Example |
|
== |
Checks
if the values of two operands are equal or not, if yes then condition becomes
true. |
(A
== B) is not true. |
|
!= |
Checks
if the values of two operands are equal or not, if values are not equal then
condition becomes true. |
(A
!= B) is true. |
|
> |
Checks
if the value of left operand is greater than the value of right operand, if
yes then condition becomes true. |
(A
> B) is not true. |
|
< |
Checks
if the value of left operand is less than the value of right operand, if yes
then condition becomes true. |
(A
< B) is true. |
|
>= |
Checks
if the value of left operand is greater than or equal to the value of right
operand, if yes then condition becomes true. |
(A
>= B) is not true. |
|
<= |
Checks
if the value of left operand is less than or equal to the value of right
operand, if yes then condition becomes true. |
(A
<= B) is true. |
3.
Logical Operators:
They are used to combine
two or more conditions or to complement the evaluation of the original
condition in consideration.
Following table shows all
the logical operators supported by C#. Assume variable A holds Boolean value
true and variable B holds Boolean value false, then
|
Operator |
Description |
Example |
|
&& |
Called Logical AND operator. Returns
True if both statements are true. |
(A && B) is false. |
|
|| |
Called Logical OR Operator. Returns
True if one of the statements is true. |
(A || B) is true. |
|
! |
Called Logical NOT Operator. Use to
reverses the logical state of its operand. If a condition is true then
Logical NOT operator will make false. |
!(A && B) is true. |
4.
Assignment operators:
v Assignment
operators are used to assigning a value to a variable.
v The
left side operand of the assignment operator is a variable and right side
operand of the assignment operator is a value.
v The
value on the right side must be of the same data-type of the variable on the
left side otherwise the compiler will raise an error.
There are following
assignment operators supported by C# −
|
Operator |
Description |
Example |
|
= |
Simple assignment operator, Assigns values from
right side operands to left side operand |
C = A + B assigns value of A + B into C |
|
+= |
Add AND assignment operator, It adds right
operand to the left operand and assign the result to left operand |
C += A is equivalent to C = C + A |
|
-= |
Subtract AND assignment operator, It subtracts
right operand from the left operand and assign the result to left operand |
C -= A is equivalent to C = C - A |
|
*= |
Multiply AND assignment operator, It multiplies
right operand with the left operand and assign the result to left operand |
C *= A is equivalent to C = C * A |
|
/= |
Divide AND assignment operator, It divides left
operand with the right operand and assign the result to left operand |
C /= A is equivalent to C = C / A |
|
%= |
Modulus AND assignment operator, It takes modulus
using two operands and assign the result to left operand |
C %= A is equivalent to C = C % A |
5.
Conditional or Ternary Operator:
C# includes a
decision-making operator ?: which is called the
conditional operator or ternary operator. It is the short form of the if else
conditions.
Syntax:
condition ? statement 1 :
statement 2
The ternary operator starts
with a boolean condition.
If this condition evaluates
to true then it will execute the statement1 after?, otherwise the statement2
after : will be executed.
Comments
Post a Comment