You are on page 1of 32

OPERATORS AND EXPRESSIONS IN

C++
TYPES OF OPERATORS
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C++ is rich in built-in
operators and provides the following types of operators:
Arithmetic Operators
Increment/ Decrement
Relational Operators
Logical Operators
Conditional Operator
sizeof Operator
Comma Operator
Assignment Operator
Arithmetic Operators
Unary arithmetic operators
Operator Symbol Form Operation
Unary plus + +x Value of x
Unary minus - -x Negation of x
Binary arithmetic operators
Assume variable A holds 10 and variable B holds 20, then:
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will g ive 200
/ Divides numerator by de-numerator B / A will g ive 2
% Modulus Operator gives remainder after an B % A will give 0
integer division
Division operator is having two different modes.
Integer and floating point division
If both of the operands are integers, the division operator
performs integer division.
For example, 7 / 3 = 2
If either or both of the operands are floating point values, the
division operator performs floating point division.
For example, 7.0 / 3 = 2.333, 7 / 3.0 = 2.333, and 7.0 / 3.0 =
2.333
Modulus (remainder)
The modulus operator only works on integer operands, and
it returns the remainder after doing integer division.
For example 7 % 3 = 1
Increment/ Decrement Operator (++/--)
Increment and decrement operators work only with integer
variables -- not on floating point variables or literals.
Increment operator, increases integer value by one
Decrement operator, decreases integer value by one
There are actually two version of each operator a prefix version
and a postfix version.
Prefix :- ++x ; --x;
Postfix:- x++; x--;
When used alone
++x; => x++; => x=x+1;
- -x; => x- -; => x=x-1;
When an increment or decrement is used as part of an
expression, there is an important difference in prefix and
postfix forms.
Postfix:
i++;
i--;
increment or decrement occurs after the variable's value is used in
the remainder of the expression.
Rule is use and then change the value in an expression
int amount, count ;
count = 3 ;
amount = 2 * count++ ;
amount gets the value of 2 * 3, which is 6, and then 1 gets
added to count
So, after executing the last line, amount is 6 and count is 4.
Prefix:
++i;
--i;
increment or decrement occurs before the variable's value is used
in the remainder of the expression.
Rule is change the value and then use in an expression
int amount, count ;
count = 3 ;
amount = 2 * ++count ;
1 gets added to count first, then amount gets the value of 2 * 4,
which is 8.
So, after executing the last line, amount is 8 and count is 4
Code:- ( Give final value of answer and value )
int answer, value = 4;
value = value + 1 ;
value++ ;
++value ;
answer = 2 * value++ ;
answer = ++value / 2 ;
value-- ;
--value ;
answer = --value * 2 ;
answer = value-- / 3 ;
Relational Operators
The relational operators or
comparison operators are often used
to create a test expression that
controls program flow. This type of
expression is also known as a Boolean
expression. Each of these operators
evaluates to the Boolean value true (1),
or false (0).
Evaluate the following Boolean
expressions:
9 < 25
9 < 3
9 > 14
9 <= 17
9 >= 25
9 == 13
9 != 13
Important note:-
The relational operators have a lower
precedence than the arithmetic operators.
I+ 5 >= j-6
(I+5) >= (j-6)
Avoid equality and inequality comparisons
on floating point numbers
Avoid comparing signed and unsigned
values.
Avoid equality and inequality comparisons on floating point
numbers
Floating point math is not exact. Simple values like
0.2 cannot be precisely represented using binary
floating point numbers, and the limited precision of
floating point numbers means that slight changes in
the order of operations can change the result (due to
round off error.). Different compilers and CPU
architectures store temporary results at different
precisions, so results will differ depending on the
details of your environment.
Avoid comparing signed and unsigned values.
In general, arithmetic that combines signed and unsigned
values yields an unsigned result. This property is particularly
important for comparisons, in which a signed integer is
converted to unsigned for comparison purposes.
E.G:-
A=3 b=-4 (consider 4 bit coding )
A< b ( 3<-4 should return 0 but returns 1)
Reason :-
3= 0011
-4 = 1100 ( 1 for representing negative no. its a sign
bit )
But signed int will be treated as unsigned int so
instead of 4 it will be 12 hence the final result is 1
Logical Operators
The logical operators are used to combine
multiple conditions formed using relational or
equality expressions. This type of expression is
also known as a Boolean expression because
they create a Boolean answer or value when
evaluated.
Note:-
C++ consider 0 as false value and any non
zero value as true value even if its positive or
negative
! is Unary operator i.e. it requires only single operand
Note:-
Logical OR and Logical AND operator has lower
precedence than any of relational operators
Negation operator (NOT) has a higher
precedence than any of relational or arithmetic
operators.
e.g :- x= 4
!(x> 5) = 1
!x > 5 => (!x) > 5 => 0 > 5 => 0
Precedence order between logical operators is
NOT (!) highest
AND (&&)
OR (|| ) lowest
Conditional / ternary Operator ( ? :)
The conditional operator evaluates an
expression, returning one value if that
expression evaluates to true, and a different
one if the expression evaluates as false. Its
syntax is:
condition ? result1 : result2
If condition is true, the entire expression
evaluates to result1, and otherwise to result2.
e.g:-
7==5 ? 4 : 3
7==5+2 ? 4 : 3
5>3 ? a : b
a>b ? a : b
Class1>=10?(marks>=80 ? A: B):C;
Note:- Conditional operator has low
precedence
sizeof operator (Unary compile time operator)
This operator accepts one parameter, which can be
either a type or a variable, and returns the size in
bytes of that type or object:
Syntax:-
sizeof var
sizeof (type )
Example
cout << "Size of int : " << sizeof(int) << endl;
float x
cout << "Size of int : " << sizeof x << endl;
Comma operator (,)
The purpose of comma operator is to string
together several expressions. The value of a
comma-separated list of expressions is the
value of the right-most expression.
Expressions are evaluated left to right in
sequence.
Example
var = (count=19, incr=10, count+1);
o/p :- var = 20
Assignment operator (=)
The assignment operator assigns a value to a variable.
x = 5;
Assignment cab be chained together.
x=y=z=13;
(Note :- declare all the variables )
C++ provides a shorthand notation. Any of the operators "+"
(addition), "-" (subtraction), "*" (multiplication), "/"
(division) and "%" (modulus) can be prefixed to the
assignment operator (=),
Expressions
An expression in C++ is any valid
combination of operators, constants
and variables.
Arithmetic Expressions:- Result is
number either integer or float
Logical Expressions :- Result is true
or false
Most of the mathematical functions are placed in math.h header
Type Conversion
C++ facilitates type conversion in two
forms:-
Implicit type conversion
(Type Promotion / automatic
type conversion)
Explicit type conversion
(Type Casting )
Implicit type conversion is done automatically by the compiler
whenever data from different types is intermixed.
The C++ compiler convert all operands upto type of largest operand,
which is called type promotion.
Hierarchy of data types:
long double (highest)
double
float
unsigned long int
long int
unsigned int
int
char (lowest)
For example, in the expression 2 + 3.14159, the + operator requires
both operands to be the same type. In this case, the left operand is an int,
and the right operand is a double. Because double is higher in the
hierarchy, the int gets converted to a double. Consequently, this
expression is evaluated as 2.0 + 3.14159, which evaluates to 5.14159.
Char and short are always implicitly promoted to integers (or unsigned
integers)
Type Casting represents a request by the
programmer to convert value of one type to another
type. It is forced type conversion. casts are done via
the () operator, with the name of the type to cast to
inside.
Eg:-
int nValue1 = 10;
int nValue2 = 4;
float fValue = (float)nValue1 / nValue2;
Type Compatibility
The value of the right side (expression side ) of the
assignment is converted to the type of the left side
(target value )
When conversion take place from smaller type to
higher type e.g. from int to float or float to double
there is no loss of information
double dValue = 3; // implicit conversion to double value
3.0
Otherwise from higher type to lower type leads to
loss of information.
int nValue = 3.14156; // implicit conversion to integer
value 3

You might also like