Types of Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment & decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
9. Manipulators
10. Memory allocate / delete Operators
An expression is a combination of variables, constants and operators written according to the syntax of the language.
Arithmetic Operators
C++ has both unary & binary arithmetic operators.
• Unary operators are those, which operate on a single operand.
• Whereas, binary operators on two operands +, -, *, /, % Examples for Unary Operators:
int x = 10;
int y = -x; (The value of x after negation is assigned to y ie. y becomes –10.) int x = 5;
int sum = -x;
Examples for binary Operators: int x = 16, y=5;
x+y = 21; (result of the arithmetic expression), x-y = 11; x*y=80;
/ - Division Operator
Eg:
x = 10, y = 3;
x/y=3; (The result is truncated, the decimal part is discarded.)
% - Modulo Division
The result is the remainder of the integer division only applicable for integer values. x=11, y = 2 x%y = 1
Relational Operators
• A relational operator is used to make comparison between two expressions.
• All relational operators are binary and require two operands.
• <, <=, >, >=, ++, !=
Relational Expression
Expression1 relational operator Expression2
Expression1 & 2 – may be either constants or variables or arithmetic expression.
Eg:
a < b (Compares its left hand side operand with its right hand side operand) 10 = = 15
a != b
♦ An relational expression is always return either zero or 1, after evaluation. Eg: (a+b) <= (c+d)
arithmetic expression
Here relational operator compares the relation between arithmetic expressions.
Logical Operators
& -Logical AND
! -Logical OR
! -Logical NOT
Logical operators are used when we want to test more than one condition and make decisions.
Eg: (a<b) && (x= =10)
An expression of this kind, which combines two or more relational expressions, is termed as a logical expression. Like simple relational expressions, a logical expression also yields a value of one or zero, according to the truth table.
Operand 1
|
Operand 2
|
AND
|
OR
|
NOT OP1
|
NOT OP2
|
0
|
0
|
0
|
0
|
1
|
1
|
0
|
1
|
0
|
1
|
1
|
0
|
1
|
0
|
0
|
1
|
0
|
1
|
1
|
1
|
1
|
1
|
0
|
0
|
Assignment Operators
Assignment operators are used to assign the result of an expression to a variable.
Eg: a = 10;
a = a + b; x = y;
Chained Assignment:
Syntax: Variable operator = operand
OP = is called as shorthand assignment operator. VOP = exp
is equivalent to v = v op exp Eg: x+=y; x = x+y;
Increment & Decrement Operators
• Increment ++, this operator adds 1 to the operand
• Decrement --, this operator subtracts 1 from the operand
• Both are unary operators
Eg:
m = 5;
y = ++m; (adds 1 to m value)
x = --m; (Subtracts 1 from the value of m)
Types
• Pre Increment / Decrement OP
• Post Increment / Decrement OP
If the operator precedes the operand, it is called pre increment or pre decrement.
Eg: ++i, --i;
If the operator follows the operand, it is called post increment or post decrement. Eg: ++i, --i;
In the pre Increment / Decrement the operand will be altered in value before it is utilized for its purpose within the program.
Eg: x = 10; Y = ++x;
• 1st x value is getting incremented with 1.
• Then the incremented value is assigned to y.
In the post Increment / Decrement the value of the operand will be altered after it is
utilized.
Eg:
• 1st x value is getting assigned to x & then the value of y is getting increased.
General Form is
Conditional Operator:
Conditional exp ? exp 1 : exp 2;
Conditional exp - either relational or logical expression is used. Exp1 & exp 2 : are may be either a variable or any statement.
Eg:
(a>b)?a:b;
• Conditional expression is evaluated first.
• If the result is ‘1’ is true, then expression1 is evaluated.
• If the result is zero, then expression2 is evaluated.
Eg: lar = (10>5)?10:5;
Bitwise Operators
Used to perform operations in bit level
Operators used:
& -Bitwise AND
| - Bitwise OR
^ -Exclusive OR
< -Left shift
> -Right shift
~ - One’s complement
Special Operators
• sizeof
• comma(,)
• size of operators returns the size the variable occupied from system memory. Eg: var = sizeof(int)
cout<<var;
|
Ans: 2
|
x = size of (float);
| |
cout << x;
|
Ans: 4
|
int y; x = sizeof (y);
| |
cout<<y;
|
Ans: 2
|
Precedence of operators:
Name
|
Operators
|
Associativity
|
Unary
|
-,++,--,!, sizeof
|
R à L
|
Mul, div & mod
|
*, /, %
|
L à R
|
Add, Sub
|
+, -
|
L à R
|
Relational
|
<. <=, >, >=
|
L à R
|
Equality
|
= =, !=
|
L à R
|
Logical AND
|
&&
|
L à R
|
Logical OR
|
||
|
L à R
|
Example
x =10, y = 2m z = 10.5
a = (x+y) – (x/y)*z;
= 12 – 5 * 10.5;
= 12 – 52.5;
a = -42.5
Manipulators
Manipulators are operators used to format the data display. The commonly used manipulators are endl, setw.
endl manipulators
It is used in output statement causes a line feed to be inserted, it has the same effect as using the newline character “\n” in ‘C’ language.
#include <iostream.h> main()
{
int a=10, b=20;
cout << “C++ language” << endl; cout << “A value: “ << a << endl; cout << “B value:” <<
b << endl;
}