WHAT ARE OPERATORS AND WHY DO WE NEED IT?
Operators are used to compute
results and compare the data values of a program. A program often involves decisionmaking and
iterative tasks (repetitive tasks). You
can use conditional constructs for decisionmaking and conditional constructs
for repetitive for decision making and repetitive tasks in your programs.
This post discusses the various
types of operators used in the C# language.
In addition, the chapter discusses the various conditional and looping
(repetition) constructs supported by C#.
Using Operators
Consider an example, where you need
to develop a C# program for displaying the result and rank of the students in a
class. To compute result and ranks, the
application needs to perform some calculations and comparisons. You can use various operators in your C#
program and use them to compute the results and ranks of the students.
Operators like + (addition) and
–(subtraction) are used to process variables and return a value. An operator is a set of one or more
characters that is used for computations or comparisons. Operators can change one or more data values,
called operands, into a new data value.
Consider the following example:
X+Y

The preceding expression uses two operands, x and y, and an
operator, +, to add the values of both the variables. The following figure shows the operator and
operands used in the preceding expression.
You can use the following types of operators
in your C# programs:
·
Arithmetic operators
·
Arithmetic
assignment operators
·
Unary operators
·
Comparison Operators
·
Logical Operators
ARITHMETIC OPERATORS
Arithmetic operators are used to
perform arithmetic operations on variables.
The following table describes the commonly used arithmetic operators.
OPERATOR

DESCRIPTION

EXAMPLE

+

USED TO ADD TWO NUMBERS

X=Y+Z



USED TO SUBTRACT TWO NUMBERS

X=YZ

*

USED TO MULTIPLY TWO NUMBERS

X=Y*Z

/

USED TO DIVIDE ONE NUMBER BY ANOTHER

X=Y/Z

%

USED TO DIVEIDE TWO NUMBERS AND RETURN THE REMAINDER. THE OPERATOR IS CALLED AS MODULUS OPERATOR

X=Y%Z

ARITHMETIC ASSIGNMENT OPERATORS
Arithmetic assignment operators are
used to perform arithmetic operations on two given operands and to assign the
resultant value to any one of them
The following table lists the usage
and describes the commonly used arithmetic assignment operators.
OPERATOR

USAGE

DESCRIPTION

=

X=5;

Store the value x in 5

+=

X+=5;

Same as X=X+5

=

X=5;

Same as X=X5

*=

X*=5;

Same as X=X*5

/=

X/=5;

Same as X=X/5

%=

X%=Y;

Same as X=X%5

UNARY OPERATORS
Unary operators are used to
increment or decrement the value of an operand by 1. The following table explains the usage of the
increment and decrement operators.
OPERATOR

USAGE

DESCRIPTION

EXAMPLE

++

++Operand;
(pre increment operator) or, operand++;
(post increment)

Used to increment the value of an operand by 1

Y=++X;
If the initial value of X is 5, after the preceding statement, values
of both X and Y will be 6. This is
because it will first increment the value of x by 1 and then assign it to Y.
Y=X++;
If the initial value of x is 5, after the execution of the
preceding statement, value of x will
be 6 and the value of y will be 5.
This is because it will first assign the value of x to y and then
increment the value of X by 1.



Operand;
(Pre decrement operator)
Or,
Operand;
(Post decrement operator)

Used to decrement the value of an operand by 1

Y=X;
If the initial value of x is 5, after the execution of the preceding
statement, values of X and Y will be 4.
This is because it will first decrement the value of X by 1 and then
assign the value to Y.
Y=X;
If the initial value of X is
5, after the execution of the preceding statement, value of X will be 4, and
the value of Y will be 5. This is
because it will first assign the value of X to Y and then decrement the value
of X by 1.

LOGICAL OPERATORS
Logical operators are used to
evaluate expressions and return a Boolean value. The following table explains the usage of
logical operators.
OPERATOR

USAGE

DESCRIPTION

EXAMPLE

&&

Expression1 &&
Expression2

Returns true if both expression1 and expression2 are true

Bool result;
String str1, str2;
str1 =”Korea”;
str2=”France”;
Result= (str1=”Korea”) && (str2=”France”);
Console.WriteLine(result.ToString());
The preceding code snippet will print “True” on the screen because
str1 has the value “Korea” and str2 has the value “France”

!

! Expression

Returns true if the expression is false.

Bool result;
Int x;
X=20;
Result=(! (x==10)) && (str2=”France”);
Console.WriteLine(result.ToString());
The preceding code snippet will print “True” on the screen because
str1 has the value “(x==10)”



Expression1

Expression2

Returns true if either of expression1 and expression2 are true or both
of them are true

Bool result;
String str1, str2;
str1 =”Korea”;
str2=”France”;
Result= (str1=”Korea”) (str2=”France”);
Console.WriteLine(result.ToString());
The preceding code snippet will print “True” on the screen because
str1 has the value “Korea” or str2 has the value “France”

^

Expression1
^
Expression2

Returns true if either of
expression1 and expression2 are true
It returns false if both expression1 and expression2 are true or if
both expression1 and expression2 are false

Bool result;
String str1, str2;
str1 =”Korea”;
str2=”France”;
Result= (str1=”Korea”) ^(str2=”France”);
Console.WriteLine(result.ToString());
This will print “False” on the screen because both the expression are
“True”

COMPARISION OPERATOR
Comparison operators are used to
compare two values and perform an action on the basis of the result of that comparison. Whenever you use comparison operator, the
expression result in a Boolean value, ‘true’ or ‘false’. The following table explains the usage of
some commonly used comparison operators.
Operator

Usage

Description

Example
(In the following examples, the value of X is assumed to be 20 and
the value of Y is assumed to be 25)

<

Expression1
<
Expression2

Used to check whether Expression1 is less than Expression2

Bool Result;
Result = X<Y;
Result will have the value true

>

Expression1
>
Expression2

Used to check whether Expression1 is more than Expression2

Bool Result;
Result = X>Y;
Result will have the value false

<=

Expression1
<=
Expression2

Used to check whether Expression1 is less than or equal to Expression2

Bool Result;
Result = X<=Y;
Result will have the value true

=>

Expression1
<=
Expression2

Used to check whether Expression1 is greater than or equal to Expression2

Bool Result;
Result = X=>Y;
Result will have the value false

==

Expression1
==
Expression2

Used to check whether Expression1 is equal to Expression2

Bool Result;
Result = X==Y;
Result will have the value false

=!

Expression1
!=
Expression2

Used to check whether Expression1 is not equal to Expression2

Bool Result;
Result = X!=Y;
Result will have the value true

No comments:
Post a Comment