Operators:
Operators
are symbols which are used to perform arithmetic, conditional and logical
operations or manipulations.
There are
the following operators in C++:
1. Arithmetic
operators
2. Comparison operators
3. Logical operators
4. Other operators
1.
Arithmetic
operators:
Arithmetic
operators are used to performing arithmetic operations such as addition,
subtraction, multiplication and division.
Operator
Name
|
Description
|
Operation
|
+
|
Used to add operands.
|
2 + 3 = 5
|
-
|
Used to subtract operands.
|
4 - 2 = 2
|
*
|
Used to find product of operands.
|
7 * 3 = 21
|
/
|
Used to perform division.
|
12 / 4 = 3
|
%
|
Provide the reminder of the
division.
|
12 / 4 = 0
|
2.
Comparison
operators:
Comparison
operators compare two values or operands to check whether it is greater, less
or equal.
Operator name
|
Description
|
Operation
|
<
|
Used to
find the smaller value
|
IF( A < B ) then
TRUE
|
>
|
Used to
find the greater value
|
IF( A > B) then TRUE
|
<=
|
Used to
find less than or equal value
|
IF(A <= B) then TRUE
|
>=
|
Used to
find greater than or equal value
|
IF( A >= B ) then TRUE
|
!=
|
Used to
find not equal value
|
IF (A != B) then TRUE
|
==
|
Used to
find equal value
|
IF ( A == B) then TRUE
|
3.
Logical
Operators:
Logical
operators are used to apply logical functions which are:
- AND ( && )
- OR ( || )
- NOT ( ! )
AND:
AND Operator
works with at least two operands:
X
|
Y
|
X AND Y
|
0
|
0
|
0
|
1
|
0
|
0
|
0
|
1
|
0
|
1
|
1
|
1
|
It evaluates the
result of two conditions if the both conditions are true otherwise false.
Here is an
example to understand it:
If ((A > B)
&& (A > C))
cout << A << ” is
greater ”;
In the above
example if the value of A is greater than B and greater than C the result will
be A.
OR:
OR operator is
also a logical operator which evaluates the result if one of them conditions is
true
X
|
Y
|
X OR Y
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
1
|
1
|
1
|
1
|
1
|
Here is an
example to understand it:
If ((A > B )
|| ( A < C))
cout << A << ” is
greater ”;
In the above
example the result will be value of “A” if A is greater than B or C.
NOT
NOT operator is
the negation of the result it shows the result true if condition is false and
wise versa.
X
|
!(X)
|
0
|
1
|
1
|
0
|
0
|
1
|
1
|
0
|
Here is an
example to understand it:
If ( !( A > B )
)
cout << B << ” is
greater ”;
In the above
example if the value of A is greater than B the NOT operator will change the
result and shows that B is greater than A.
4.
Other
Operators:
These are
the some other operators which are also used:
·
Assignment operator
·
Compound assignment operator
·
Increment/decrement
·
Ternary operator “?”
Assignment
operator:
Assignment
operator is used to assign a value to a variable.
Int A=10;
Compound
assignment operator:
Compound
assignment operators are used to assign value as well as operation with value.
A+=10 à A = A + 10;
Increment/decrement
Increment
operator increases the value of a variable by 1 and decrement decreases the
value of the operator by 1
A=10;
B = ++A;
|
Here
the value of B is 11 because firstly it is incremented by 1 and then assigned
to B.
|
C = --A;
|
Here
the value of C is 9 because firstly it is decremented by 1 and then assigned
to C.
|
X = A--;
|
Here the value of X is 10 because firstly the value of A is
assigned to X and then decremented
by 1.
|
Y= A++;
|
Here the value of Y is 10 because firstly the value of A is
assigned to Y and then incremented by 1.
|
Ternary
operator
The ternary
operator is used to evaluating a condition. It returns one value of result
depends on the condition.
Condition ?
“result1” : “result2” ;
If the condition
is true the output will result1 otherwise result2.
Example:
A = ( 7 > 2 )
“True” : “False” ;
The value of A is
true because the condition is true and output is “True”.
No comments:
Post a Comment