4. Operators
• A set of symbols that are used to perform an operation on values.
• C++ provides rich amount of operators such as
• Assignment and arithmetic operators
• Increment/decrement and arithmetic assignment operators
• Relational and comparison operations
• Logical and conditional operators
• Bitwise operators
• Manipulators and other miscellaneous operators
5. Assignment Operator
• This operator is used for assigning a
value to a variable.
• Assignment operator is =.
• Assigning the value/placing the value
in memory location – destructive and
reading back is called non-destructive.
int a, b, c = 10;
a = 25;
b = a;
6. Arithmetic Operators
• These operators are used to perform
arithmetic operations on the data such
as addition, subtraction, multiplication,
division and modulo operations.
• They can operate on both variables
and constants.
int a, b, c = 10;
a = 25;
c = a;
b = a + b;
// b = a – b;
// b = a * b;
7. Arithmetic Assignment Operators
• These operators are used to modify
the current value of a variable by
performing an operation on it.
• Arithmetic operation
• Assignment operation
• Arithmetic assignment operators are
+=, -=, *=, and /=.
int a, b, c = 10;
a = 50;
a += 40; //a = 90
b = 90;
B -= 40; //50;
9. Increment and Decrement Operators
• These operators are used to add or
subtract one value from the current
value of a variable.
• The increment operator is represented
by ++ and the decrement is
represented by --.
• It can be used either as postfix or
prefix notation.
int a = 50, b, c;
b = a + (++a); //prefix
c = a + (a++); //postfix
10. Relational or Comparison Operators
• These operators are used to show
relation among number.
• Common relational operators are >, <,
>=, <=, ==, and !=.
• The result can be either true (1) or
false (0).
int a = 50, b = 40;
cout<<(a<b); //return 0
cout<<(a>b); //return 1
11. Logical Operators
• These operators are used to combine
condition within a single statement.
• They are also called compound
operators.
• Logical operators are &&, ||, and !.
int a = 50, b = 40;
cout<<((a<b) && (a <80));
cout<<(a!=20);
13. Conditional or Ternary Operators
• These operators are used to
return a value when a desired
condition is true or false.
• The common conditional
operators are ? And :.
int a = 50;
cout<<((a>10)?”T”:”F”);
14. Bitwise Operators
• These operators are used modify variables considering the bit
patterns that represent the values they store.
15. Manipulators
• These operators are used in C++
for formatting output.
• More commonly manipulators
are endl and setw.
• endl is used for line break.
• setw is used to set the width of an
output.
cout<“Welcome to”<<endl;
cout<<“Jahan
University”<<endl;
cout<<setw(8)<<“Name”<<se
tw(20)<<“Fname”<<endl;
16. Order of Precedence
• The order in which the arithmetic expression is evaluated is called the
order of precedence.
• Also known as hierarchy of operations.
• C++ expressions are performed in the following order:
• All multiplication and divisions are performed first from left to right.
• All additions and subtractions are then performed from left to right.
• If parenthesis are used in expression, the expressions are first computed.
• When parenthesis are used within parenthesis, the expression with
innermost parenthesis is evaluated first.