You are on page 1of 4

Lets learn c++

Chapter 3: Operators
Chapter 3: Operators........................................................................................................1
3.1 Introduction............................................................................................................1
3.2 Arithmetic Operators.............................................................................................1
3.2.1 The precedence of arithmetic operators..............................................................1
3.2.2 Increment and Decrement Operators..................................................................2
3.3 Relational and logical operators............................................................................2
3.4 Composite and assignment Operators...................................................................3
3.5 sizeof Operator.......................................................................................................4
3.6 Type conversion.....................................................................................................4

3.1 Introduction
An operator is a symbol that tells the compiler to perform a specific mathematical or
logical manipulation. C++ has the following operators:

3.2 Arithmetic Operators


Below is a list of arithmetic operators used in C++
Operator
description
+ ................Addition
- .................Subtraction
* .................Multiplication
/ .................Division
% ................Modulus / remainder operator-applied only to integer types
++...............Increment
- - ...............Decrement

3.2.1 The precedence of arithmetic operators


Highest
Lowest

++ --(Unary minus)
*,/,%
+, -

Operators at the same precedence level are evaluated from left to right. Parenthesis
may be used to alter the order of evaluation. They force an operation or a set of
operations to have a higher precedence level.

Illustration1:
int x=6,y=4,z=2;
x+y/z =8
(x+y)/z =5
Ephantus k
efantusk@yahoo.com
Tel: 0721 374616

Lets learn c++


Illustration 2:

#include <iostream.h>
int main ()
{//test operators +,-,*, / and %:
int m=54;
int n=20;
cout<< m= <<m<< and n=<< n<<endl;
cout<< m+n <<m+n<< endl;
cout<< m-n= <<m-n<< endl;
cout<< m*n= <<m*n <<endl;
cout<< m/n= <<m/n <<endl;
cout<< m%n= <<m%n <<endl;
}
NB: From the above program we can deduce that integer division truncates the
results. What would be the results if m and n were float variables?

3.2.2 Increment and Decrement Operators


Each of these operators has two versions, a pre version and a post version. The
pre version performs the operation [either adding 1 or subtracting 1] on the object
before the resulting value is used in its surrounding context. The post version
performs the operation after the objects current value has been used.
Example
int main()
{// shows the difference between m++ and ++m:
int m,n;
m=44;
n=++m;//pre-increment operator is applied to m:
cout<< m=<< m << , n=<<n<<endl;
m=44;
n=m++;// post increment operator is applied to m:
cout<< m=<< m << , n=<<n<<endl;
}
NB: Is there a difference between the first values of m and n with the second values?
Write a similar program but use the two versions of the decrement operator. What is
the output?

3.3 Relational and logical operators


Relational refers to the relationships that values can have with one another, and logical
refers to the way in which true or false values can be connected together.
C++ has the following relational and logical operators:
Relational operators
Operator.................................Meaning
>.............................................Greater than
>=...........................................Greater then or equal to
<.............................................Less than
<=...........................................Less than or equal to
Ephantus k
efantusk@yahoo.com
Tel: 0721 374616

Lets learn c++


= =..........................................Equal to
! =...........................................Not equal to
Logical operators
Operator ................................meaning
&&.........................................AND (yields true when the two operands are true)
| | ............................................OR (yields true when one of the operands is true)
!..............................................NOT (Negates the bool value)
Q. How do you construct a logical exclusive-OR?
Illustration

#include <iostream.h>
int main()
{
int x=10;
int y=5;
if(x<y) cout<< x<y\n;
if(x<=y) cout<< x<=y\n;
if(x= =y) cout<< x= =y\n;
if(x>y) cout<< x>y\n;
if(x>=y) cout<< x>=y\n;
if(x!=y) cout<< x!=y\n;
}

3.4 Composite and assignment Operators


The standard assignment operator in C++ is the equals sign [=]. In addition C++
provides the following composite assignment operators: +=, -=, *=, /= and %=.
Example
Int main()
{
int x=10;
cout<< x=<<x<<endl;
x+=12; // same as x=x+12:
cout<< After x+=12, x=<<x<<endl;
X-=7; // same as x=x-7:
cout<< After X-=7,x=<<x<<endl;
x*=4;// same as x=x*4:
cout<< After x*=4, x=<<x<<endl;
x/=2;// same as x=x/2:
cout<< After x/=2, x=<<x<<endl;

x%=8;// same as x=x%8:


cout<< After x%=8 ,x=<<x<<endl;

Ephantus k
efantusk@yahoo.com
Tel: 0721 374616

Lets learn c++


NB the assignment operator can be used to create a chain of assignments such as:
X=y=z=10;
In which case x, y and z are assigned the value 10.

3.5 sizeof Operator


The sizeof operator returns the size in bytes of the type specified. It can be used on
any computer to determine how many bytes it uses for each data type.
Example
int main ()
{// prints the storage size of the char ,int and float types:
cout<<Number of bytes used :\n;
cout<<
char: <<
sizeof(char)<<endl;
cout<<
int: <<
sizeof(int)<<endl;
cout<<
float: <<
sizeof(float)<<endl;
}
Create a program that prints the storage size of the other int and float data types.

3.6 Type conversion


In c++ type conversion from high range to low range data type happens automatically.
This is referred to as type promotion.
Illustration
#include <iostream.h>
int main()
{
int x= 2;
float y=1.5;
y=y+ x; // the value 2 is automatically converted to the value 2.0.
cout<<y <<endl;
}
However, converting a value from a high range to low range type requires type casting,
i.e., explicit type conversion.
Illustration:
#include <iostream.h>
int main()
{
int x= int (5.5); // the value 5.5 is truncated to 5 and assigned to x:
cout<<x <<endl;
return 0;
}

3.7 Exercise:
1. Write a program that prints the sum difference, product, quotient and remainder of
two integers that are input interactively.
2. Read about unary operators, typedef operator and bitwise operators.
Ephantus k
efantusk@yahoo.com
Tel: 0721 374616

You might also like