Start C# here

Study C#.. Go ahead ! !

Tuesday, March 18, 2008

What are operators?

Operators are used to do perform some operations on variables.

We have four types of operators in C#.
  • Arithmetic Operators,
  • Relational operators ,
  • Logical operators and
  • Unary operators.


Arithmetic Operators :-

Arithmetic Operators are used to do mathematical operations


The basic arithmetic Operators are

+ for addition
- for subtraction
* multiplication
/ division
% modulo operator(This operator is used to return the Remainder of two numbers).



Relational Operators:-


Relational operators are mainly used for checking conditions between two or more variables.

The relational operators we use in C# are..

> Greater than(Checks which number is greater)
<>= Greater than or equal to(Checks whether the given number is greater than or equal to the second number)
<= Less than or equal to(Checks whether the given number is less than or equal to the second number) == Equal to(Checks whether the second number is equal to the first number) != not equal to(Checks whether the second number is not equal to the first number) Logical Operators :

Logical operators are used to check logical conditions with a block of two or more relations
Logical operators we use in C# are AND, OR and NOT.

  • AND operator is represented with && (two ampersand)
  • OR operator is represented with || (two pipes) and
  • NOT operator is represented with !(Exclamation).

eg:-
((iNum1>iNum2) && (iNum1!=0))
In the above example we are checking whether the first variable is greater than the second variable AND iNum1 is not equal to ZERO. Here if both the conditions are true only the condition will execute.


How Logical operators work.

AND(&&)
&&(AND) operator becomes true only if all the conditions are true. If any of the conditions are false && operator returns false.

OR(||)
||(OR) operator becomes false only if all conditions are false. If any of the condition becomes true || operator becomes true.

NOT(!)
!(NOT) operator becomes true if the condition is false and false if the condition is true.


Unary Operators

Unary operators are used to add or subtract a variable by 1.
We have mainly two types of unary operators
Increment and
Decrement.


Increment operator is represented by ++ and decrement operator is represeted by --.
Increment operator adds a variable by one and decrement operator subracts a variable value by
one.

i++, i--.

Unary operators can be used in two ways POSTFIX and PREFIX.

In a postfix notation the unary operator will be placed after the variable(i++)
In a prefix notation the unary operator will be placed before the variable(++i)



Assignment Operator

An assignment operator is used to assign a value to a variable. An = (equal to) symbol is known as an assignment operator.

int i=10;
means assigning the integer variable i a vlaue of 10.

No comments: