Documente Academic
Documente Profesional
Documente Cultură
Lecture
6|7
Operators
n 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
Relational Operators
Logical Operators
Assignment Operators
Arithmetic Operators:
There are following arithmetic operators supported by C++ language:
Assume variable A holds 10 and variable B holds 20, then:
Operator
Description
Example
A + B will give 30
B / A will give 2
B % A will give 0
Example:
Following is the example to understand this difference:
#include<iostream>
Using namespace std;
int main()
{
int a=21;
int c ;
// Value of a will not be increased before assignment.
c = a++;
cout <<"Line 1 - Value of a++ is :"<< c << endl ;
// After expression value of a is increased
cout <<"Line 2 - Value of a is :"<< a << endl ;
// Value of a will be increased before assignment.
c =++a;
cout <<"Line 3 - Value of ++a is :"<< c << endl ;
return0;
}
When the above code is compiled and executed, it produces the following result:
Line1-Value of a++is:21
Line2-Value of a is:22
Line3-Value of ++a is:23
Example
Try the following example to understand all the arithmetic operators available in C++.
#include<iostream>
using namespace std;
int main()
{
int a=21;
int b=10;
int c ;
c = a + b;
cout <<"Line
c = a - b;
cout <<"Line
c = a * b;
cout <<"Line
c = a / b;
cout <<"Line
c = a % b;
cout <<"Line
c = a++;
cout <<"Line
c = a--;
cout <<"Line
return0;
}
5 - Value of c is
When the above code is compiled and executed, it produces the following result:
Line1-Value
Line2-Value
Line3-Value
Line4-Value
Line5-Value
Line6-Value
Line7-Value
of
of
of
of
of
of
of
c
c
c
c
c
c
c
is:31
is:11
is:210
is:2
is:1
is:21
is:22
Example:
Relational Operators:
There are following relational operators supported by C++ language
Assume variable A holds 10 and variable B holds 20, then:
Operator
Description
Example
==
(A == B) is not true.
!=
>
<
(A < B) is true.
>=
<=
(A <= B) is true.
Example
Try the following example to understand all the relational operators available in C++.
#include<iostream>
Using namespace std;
int main ()
{
int
int
int
if(
{
a
b
c
a
=21;
=10;
;
== b )
cout <<"Line
} else
{
cout <<"Line
}
if( a < b )
{
cout <<"Line
} else
{
cout <<"Line
} if( a > b )
{
cout <<"Line 3 - a is greater than b"<< endl ;
} else
{
cout <<"Line 3 - a is not greater than b"<< endl ; }
/* Let's change the values of a and b */
a =5;
b =20; if( a <= b )
{
cout <<"Line 4 - a is either less than \
or equal to b"<< endl ;
}
if( b >= a )
{
cout <<"Line 5 - b is either greater than \
or equal to b"<< endl ;
} return0;
}
When the above code is compiled and executed, it produces the following result:
Line1- a is not equal to b
Line2- a is not less than b
Line3- a is greater than b
Logical Operators:
Expressions that use conditional statements and logical operators. There are following logical
operators supported by C++ language.
Assume variable A holds 1 and variable B holds 0, then:
Operator Description
Example
Called Logical AND operator. A && B is true if and only if both A and B are true
&&
(A && B) is false.
Called Logical OR Operator. A || B is true if either A or B are true
||
(A || B) is true.
Example
#include<iostream>
Using namespace std;
int main()
{
int a =5;
int b =20;
int c ;
if( a && b )
{
cout <<"Line 1 - Condition
}
if( a || b )
{
cout <<"Line 2 - Condition
}
/* Let's change the values of a
a =0;
b =10; if( a && b )
{
cout <<"Line 3 - Condition
} else
{
cout <<"Line 4 - Condition
}
if(!(a && b))
{
cout <<"Line 5 - Condition
} return0;
}
is true"<< endl ;
is true"<< endl ;
and b */
is true"<< endl ;
is not true"<< endl ;
is true"<< endl ;
When the above code is compiled and executed, it produces the following result:
Line1-Condition
Line2-Condition
Line4-Condition
Line5-Condition
is
is
is
is
true
true
not true
true
Description
Example
+=
C += A is equivalent to C = C + A
-=
*=
C *= A is equivalent to C = C * A
/=
C /= A is equivalent to C = C / A
%=
Example
Try the following example to understand all the assignment operators available in C++.
#include<iostream>
using namespace std;
int main()
{
int a =21;
int c;
c = a;
cout <<"Line 1 - =
c += a;
cout <<"Line 2 - += Operator, Value of c = : "<<c<< endl ;
c -= a;
cout <<"Line 3 - -= Operator, Value of c = : "<<c<< endl ;
c *= a;
cout <<"Line 4 - *= Operator, Value of c = : "<<c<< endl ;
c /= a;
cout <<"Line 5 - /= Operator, Value of c = : "<<c<< endl ;
c =200;
c %= a;
cout <<"Line 6 - %= Operator, Value of c = : "<<c<< endl ;
c <=2;
cout <<"Line 7 - <<= Operator, Value of c = : "<<c<< endl ;
c >=2;
cout <<"Line 8 - >>= Operator, Value of c = : "<<c<< endl ;
c =2;
return0;
}
When the above code is compiled and executed, it produces the following result:
Line1-=Operator,Value of c =:21
Line2-+=Operator,Value of c =:42
Line3--=Operator,Value of c =:21
Line4-*=Operator,Value of c =:441
Line5-/=Operator,Value of c =:21
Line6-%=Operator,Value of c =:11
Line7-<<=Operator,Value of c =:44
Line8->>=Operator,Value of c =:11
Sizeof() operator:
The sizeof is a keyword, that determines the size, in bytes, of a variable or data type.
The syntax of using sizeof is as follows:
sizeof(data type)
Where data type is the desired data type including int, long, double etc.
Try the following example to understand all the sizeof operator available in C++. Copy and paste the following C++
program in test.cpp file and compile and run this program.
#include<iostream>
usingnamespace std;
int main()
{
cout <<"Size of char : "<<sizeof(char)<< endl;
cout <<"Size of int : "<<sizeof(int)<< endl;
cout <<"Size of float : "<<sizeof(float)<< endl;
cout <<"Size of double : "<<sizeof(double)<< endl;
cout <<"Size of Long double : "<<sizeof(long double)<<
endl;
return0;
}
When the above code is compiled and executed, it produces the following result, which can vary from machine to
machine:
Size
Size
Size
Size
Size
of
of
of
of
of
char:1
int:4
float:4
double:8
Long double:10
Nevertheless, on different machines the size of any data type can vary. This is because of the difference in hardware
architecture and compiler. In below example long double data type value is 16 byte. However, on linux it is 12 byte,
while on Visual studio it is 8 byte. Though, some book claims that long double data type value is 10 byte. Therefore, it
varies from 8 to 16 byte on different platforms and machines.
Output is:
Example
Try the following example to understand operators precedence concept available in C++.
Check the simple difference with and without parenthesis. This will produce different results because (), /, * and + have
different precedence. Higher precedence operators will be evaluated first:
#include<iostream>
using namespace std;
int main()
{
int a =20;
int b =10;
int c =15;
int d =5;
int e;
e =(a + b)* c / d;// ( 30 * 15 ) / 5
cout <<"Value of (a + b) * c / d is :"<< e << endl ;
e =((a + b)* c)/ d;// (30 * 15 ) / 5
cout <<"Value of ((a + b) * c) / d is
return0;
}
When the above code is compiled and executed, it produces the following result:
Value
Value
Value
Value
of
of
of
of
(a + b)* c / d is:90
((a + b)* c)/ d is:90
(a + b)*(c / d)is:90
a +(b * c)/ d is:50
Expressions:
Operators
E.g. *, +, /, - etc.
Operand
c = 7 + 8; // c=15
Escape Sequence: