Ads 468x60px

Sunday 30 December 2012

Operators

Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Conditional Operators
Comma Operators


Arithmetic Operators
There are five types of operators
Addition ( + )
Subtraction ( - )
Multiplication ( * )
Division ( / )
Modulo Division ( % )

We are very familiar with the first four operators, they are simple mathematics and work same so explaining them is really not necessary, we need to know only about modulo division because this is what you may not familiar with.

Modulo Division
It will return the remainder.
e.g. 4%2 = 0
we can not use decimal value in it. Using decimal value will give an error.

Relational Operator
The operators which compares two values. There are six relational operators
Less Than ( < )
Greater Than ( > )
Less Than Equal to ( <=)
Greater Than Equal to ( >= )
Equal to Equal to ( = = )
Not Equal to ( !=)

The definitions are already clear from the names.

Logical Operators
Three type of logical operators:
AND (&&)
OR ( || )
NOT ( ! )

AND
If you are using this then every statement must be true for the execution of the next statement.
e.g.
a=10
b=10
if(a==10 && b == 10)
cout<<"true";

OR
For this operator any single statement must be true.
e.g.
a=10
if(a==10 || a==5)
cout<<"true";

NOT
In this statement any of the statements must not true.

Assignment Operators
Their 6 types of assignment operators used to assign values.
Equal to ( = )
e.g. a = 10

Plus Equal to ( + = )
e.g. a + = 10 (a = a+10)

Minus Equal to ( - = )
e.g. a - = 10 (a = a-10)

Multiply Equal to ( * = )
e.g. a * = 10 (a = a*10)

Divide Equal to ( / = )
e.g. a / = 10 (a = a /10)

Modulo Equal to ( % = )
e.g. a % = 10 (a = a%10)

Conditional Operators
The conditional expression can be used as shorthand for some if-else statements. It is a ternary operator. This operator consist of two symbols: the question mark (?) and the colon (:).

The general syntax of the conditional operator is:
Identifier = (test expression)? Expression1: Expression2 ;

e.g.

Instead of writing
if (x < y) 
{
min = x;
}
else
{
min = y;
}

You just say...
min = (x < y) ? x : y;

Comma Operator
A set of expression is separated by this operator ( , ). 
e.g. int i,j,k;

Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!

0 comments:

Post a Comment

 
Related Posts Plugin for WordPress, Blogger...