Pages

Saturday, 15 June 2013

Using various operators in C# Part 3

                     So from our previous post we have learn about basic C# programming example writing code declaring classes, variable, accepting user input and compiling and executing the program. To read our two previous posts just click on Part 1 and Part 2.
                        In this post we will learn about various operators that are used in C#, sometimes you may need to compare data values and compute results in your programs it can’t be done without the help of operators. So learning, about operators is a good decision don’t worry they are simple and easy to understand.
          Now operators also have different categories, they are:
1)      Arithmetic operators
2)      Arithmetic assignment operators
3)      Unary operators
4)      Comparison operators
5)      Logical operators

Arithmetic operators
   
    They are used to perform basic arithmetic operation like adding and subtracting variables. The following table shows the list of operators which are used in Arithmetic operators

Operators
Use to
Examples
+
Add two numbers
X  = Y + Z;
If Y is equals to 40 and Z is equals to 4, X will be 44.
-
Subtract two numbers
X  = Y - Z;
If Y is equals to 40 and Z is equals to 4, X will be 36.
*
Multiply two numbers
X  = Y * Z;
If Y is equals to 40 and Z is equals to 4, X will be 160.
/
Divide one number by another
X  = Y / Z;
If Y is equals to 40 and Z is equals to 4, X will be 10.
%
Divide two number and return the reminder, also called module operators
X  = Y / Z;
If Y is equals to 41 and Z is equals to 4, X will be 1.





Arithmetic assignment operators
    
      Are used to perform arithmetic operations on two given operands and assigns the resultant value to any one of them. The following table shows the list of operators which are used in Arithmetic assignment operators

Operators
Use to
Examples
+=
X += Y;
Same as,
         X = X + Y;
-=
X -= Y;
Same as,
         X = X - Y;
*=
X *= Y;
Same as,
         X = X * Y;
/=
X /= Y;
Same as,
         X = X / Y;
%=
X %= Y;
Same as,
         X = X % Y;


For example take two variables X and Y where, X = 4 and Y = 6 and then apply X +=  Y; it will show X = 10 because it will add X’s value and Y’s value and assign it to X that’s why it shows X = 10 not like X = Y+Z, where it has three variables X,Y and Z and X value is equals to Y+Z.

Comparison operators

These types of operators are used to compare two expression or value and perform an action on the basis of the result of that comparison. The value of the comparison operator comes in Boolean value, true or false. The following table explains the use of some comparison operators.

(In the following example assume the value of x = 50 and the value of Y = 55)

Operators
Usage
Description
Example
< 
Expression1
< 
Expression2
It will check whether
Expression1
Is less than
Expression2
bool example;
Example = X<Y;
Example will show the value true
> 
Expression1
> 
Expression2
It will check whether
Expression1
Is greater than
Expression2
bool example;
Example = X>Y;
Example will show the value false
<=
Expression1
>=
Expression2
It will check whether
Expression1
Is less than or equals to
Expression2
bool example;
Example = X<=Y;
Example will show the value true
>=
Expression1
>=
Expression2
It will check whether
Expression1
Is greater than or equals to
Expression2
bool example;
Example = X>=Y;
Example will show the value false
==
Expression1
==
Expression2
It will check whether
Expression1
Is equals to
Expression2
bool example;
Example = X==Y;
Example will show the value false
!=
Expression1
!=
Expression2
It will check whether
Expression1
Is not equals to
Expression2
bool example;
Example = X != Y;
Example will show the value true.


Logical operators 

These types of operators evaluate an expression and return a Boolean value true or false. The following table shows and explain the use of logical operators.

Operators
Usage
Description
Example
&&
Expression1
&&
Expression2
Returns true if both
Expression1
And
Expression2
Are true
bool example;
String A1,A2;
A1 = 4;
A2 = 7;
example = (A1 == 4)&&(A2 == 7);
Console.WriteLine(example.ToString());

The above code will print true on the screen because A1 has the value 4 and A2 has the value 7.
!
! Expression

Returns true if
Expression
Is false
bool example;
int a
A1 = 10;
example = (!(A==20));
Console.WriteLine(example.ToString());

The above code will print true on the screen because the expression example = (!(A==20)); is false

||
Expression1
||
Expression2
Returns true if either
Expression1
or
Expression2
Or both of them are true
bool example;
String A1,A2;
A1 = 4;
A2 = 7;
example = (A1 == 5)||(A2 == 7);
Console.WriteLine(example.ToString());

The above code will print true on the screen because A2 has the value 7.
^
Expression1
^
Expression2
Returns true if
Expression1
or
Expression2
is true.it returns false if both
Expression1
and
Expression2
Are true or if  both
Expression1
and
Expression2
Are false

bool example;
String A1,A2;
A1 = 4;
A2 = 7;
example = (A1 == 4)^(A2 == 7);
Console.WriteLine(example.ToString());

The above code will print false on the screen because  both the expression are true.

 To understand it better you have to use these operators in a program. you can use this operators in any program but the main use of these operators are in Conditional constructs and Loop constructs we will explain them in our next two post....................