Professional Documents
Culture Documents
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:
++ --(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
#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?
#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;
}
Ephantus k
efantusk@yahoo.com
Tel: 0721 374616
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