Operator in c++:
The following operator use in c++:
- Assignment operator
- Arithmetic
operator
- Compound
assignment operator
- Increase
and decrease operator
- Relational and equality
operators operator
- Logical operators
- Conditional operator
- Comma
- Bit wise Operators
- Explicit type casting operator
Assignment operator:
assignment operator use in c++ that is " =". The assignment operator uses to assigns a
value to a variable.
e.g
b=5;
This above statement assigns the value 5 to the variable b. The left part of the assignment
operator (=) is known
as the lvalue (left value) and the right part as the rvalue (right
value). The lvalue has to be a variable whereas the rvalue can be either a
constant or variable.
e.g.
int a=5;
int b=a;
Arithmetic operator:
The five arithmetical operations use
in the C++ language which are addition(+), subtraction(-) , division(/),
multiplication(*),modulo(%). You are all familiar with first four operator in
mathematics .these four operator same work here in c++ as mathematics. The new
one for you is modulo (%) operator. Modulo is the operation that gives the
remainder of a division of two values. For example, if we write:
A=11%3;
Then variable A will contain the value 2.
Compound assignment operator:
compound assignment operator use in c++ that is (+=, -=, *=, /=, %=, >>=, <<=, &=,^=, |=) When we want to modify the value of a variable then we
can use compound assignment operators:
expression is equivalent to
------------------------------------------------------------------------
value
+= increase value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units; price
= price * units;
a%=b; a=a%b;
a^=b; a=a^b;
and same for all operator.
Increase and decrease operator:
the increase operator use in c++ that is ++ and the decrease operator use in c++
that is -- . They are
equivalent to +=1 and to -=1, respectively. It means it increase
or decrease one value to the variable.
For example:
Int a=2;
a++; //now value of a become 3.
a--; // now value of a again 2
these operator can be use as prefix or post-fix. That mean
that can be use either before the variable like(++a) or after the variable like
(a++).
The difference between them can easily understand by the
following example.
Example prefix:
B=2;
A=++B; // here
the value of A=3 and value of B=3.
Example post-fix:
B=2;
A=B++;
// here the value of A=2 and value of B=3
In Example prefix, B is
increased before its value is assign to A.
While in Example post-fix, the value of B is
first assign to A and
then B is
increased. That is the difference between prefix and post-fix in c++.
Relational and equality
operators operator:
In order to comparison between two expressions we use the relational and equality operators in
c++. Six type of operator are use in c++ which are:
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Note:
Be
careful, The operator = (one
equal sign) is not the same as the operator == (two equal signs), the first one is an
assignment operator and the other one (==)
is the equality operator.
Logical operators:
logical operator use in c++ that is ( !, &&, || ). The Operator ! is the C++ operator
is equivalent the Boolean operation ‘NOT’. It only has one operand and at right
side of operand and operator && is equivalent to Boolean operator ‘AND’.
It perform operation on two operand and operator || is equivalent to Boolean
operator ‘OR. It also takes two operand for operation. Operands mean here
variable.
&& OPERATOR
a
b a
&& b
True true true
True false
false
false true false
false false false
|| OPERATOR
a b a || b
true true true
true false true
false true true
false false false
Conditional operator :
condition operator use in c++ that is "?". It is
conditional operator in c++. It return a value if the expression is true and return
other value if the expression is false.
Syntax:
Condition ? return 1: return2;
Example:
#include <iostream>
using
namespace std;
int main ()
{
int a,b,c,d;
a=7;
b=2;
c =
(a>b) ? a : b; //here condition is true so assign value of a
to c i.e c=7
d =
(b>a) ? a : b; //here condition
is false so assign value of b to d so d=2
return 0;
}
Comma operator:
Comma operator use to
seprate the more than one expression in c++.
Example:
Int a;
Int b;
Here we have two instruction or
expression int a and int b. if we want to write it in single instruction then
we use the comma operator. Like this
Int a,b;
Bit wise Operators :
Bit wise operator modify the variable that have bit pattern in it. following bit wise operator use in c++.
-----------------------------------------------------------------------------------
operator is equivalent description
& AND Bit wise
AND
| OR Bit wise
Inclusive OR
^ XOR Bit wise
Exclusive OR
~ NOT Unary complement
(bit inversion)
<< SHL Shift Left
>> SHR Shift Right
Explicit type casting operator:
Type
casting operator allow you convert one type to another type. For this we use parentheses ();
Example:
in above example we want to
convert float in int data type. So we write the int in parentheses. So the
instruction a=(int) b;
here a is int data type and b is float data type. So the value of b is 2.6 which is floating value . so that
instruction tell the compiler assign the ‘a’ only integer value. So here assign
a=2
precedence of operator:
|
precedence of operator |