You are on page 1of 8

A’Seeb Vocational College

Introduction to programming in c++


Notes

Compiled By : Angel Deepak Godson Page 1|8


Chapter 3

Operators are special symbols which are used to perform various mathematical calculations.
Types of C++ operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators.
5. Increment and Decrement Operators

Arithmetic Operators:

 Unary arithmetic operator (Require one operand)


 Binary arithmetic operator (Require two Operand)
Unary Arithmetic:
It requires only one operand either plus and minus operator
Example: +a, -5, +6
Binary Arithmetic:
Binary arithmetic requires two operands. The table below represents the binary arithmetic
operators.

Operator Symbol Form Operation

Addition + A+B A plus B


Subtraction - A–B A minus B
Multiplication * A*B A multiplied by B
Division / A/B A divided by B
Modulus (Remainder) % A%B The remainder of A divided by B

Compiled By : Angel Deepak Godson Page 2|8


Relational Operators

Operator Description
== equal
!= Not equal
> Greater than
< Less than
>= Greater than equal
<= Less than equal

Logical Operator

Operato
Description
r
&& Called Logical AND operator
|| Called Logical OR Operator.
! Called Logical NOT Operator.

Operator Description
= Simple assignment operator
+= Add AND assignment operator Assignment operators
-= Subtract AND assignment operator
*= Multiply AND assignment operator
/= Divide AND assignment operator
%= Modulus AND assignment operator,
<<= Left shift AND assignment operator
>>= C o AND
Right shift m p assignment
i l e d B y operator
: Angel Deepak Godson Page 3|8
&= Bitwise AND assignment operator
^= bitwise exclusive OR and assignment operator
|= bitwise inclusive OR and assignment operator
Increment and Decrement Operator

WORKSHEET 1

Compiled By : Angel Deepak Godson Page 4|8


Q.1 What will be the result of the following expressions if i=10 and j = 5 initially,

a. f = ++i - j; _______________________
b. f = i++ - j; _______________________

Q.2 Give the output of the following code segment


int a = 8, b = 2, c = 7, d;
d = a++ - b * (a-c++);
cout << d; ______________________
Q.3 int a=10;
d= a++ + ++a;
cout << d; ________________________
Q.4 Evaluate the following if k=10 intially
a) (2 * ++k ) % 7 ______________________________
b) (2 * k++) % 7 ______________________________
c) --k + 4*k + 6 ______________________________
d) k-- + 4*k + 6 ______________________________

WORKSHEET 2
Q.1 Write the expressions using relational operator that following statements mean.
a. Check if x is not equal to 9.
b. Check if x is equal to y.
c. Check if d is less than or equal to e.
d. Check if t is greater than or equal to 10.

Q.2 Given,
int e1=0, e2=5
What is the result of each of the following expressions?

Compiled By : Angel Deepak Godson Page 5|8


a. e1 && e2 ________________________
b. !e1 ________________________
c. !e1 && e2 ________________________
d. e1 || !e2 ________________________

Q.3 Evaluate the following Logical Boolean expressions:

a) 25 < 7 || 15 > 36 ________________________


b) 15 > 36 || 3 < 7 ________________________
c) 14 > 7 && 5 <= 5 ________________________
d) 4 > 3 && 17 <= 7 ________________________
e) ! false ________________________
f) ! (13 != 7) ________________________
g) 9 != 7 && ! 0 ________________________
h) 5 >4 && 7 ________________________

Q.4 Using the given values of x,y and z evaluate the following.
(x == y) || (! (z == y) && (z < x))
a. x = 10, y = 10, z = 1 _____________________________
b. x = 0, y = 10, z = 10 _____________________________
c. x = 1, y = 1, z = 1 _____________________________
Q. 5 Using the given values of x,y and z evaluate the following.
(x + y) || (! (z == y) && (z < x++))
a. x = 10, y = 10, z = 1 _____________________________
b. x = 0, y = 10, z = 10 _____________________________
c. x = 1, y = 1, z = 1 _____________________________

Q. 6 Predict the output of the following code snippet.

int a=1, b =0, c;


c = a && ! b

Compiled By : Angel Deepak Godson Page 6|8


cout << c;

Q.7 Find the output of the following program snippets


a. int x=2,y;
y = ++ x;
cout << x << “ “ <<y;
cout << x++;

b. int a=2, b=4, c=6;


a = ++c + ++b / c+a;
cout << a;

Q.8 Find the error in the given expression and rewrite the corrected one.

a. int a,b,c,d;
cin >> a >> b >> c;
d = a + bc;

b. int x,y;
cin >> x >> y;
cout << x=>y;

c. float p,q;
cout << p%q;

d. int a,b;
a=+b;
cout <<a;

Compiled By : Angel Deepak Godson Page 7|8


Q.9 Write a program in c++ that calculates the cost of painting a room. Take the dimensions
of the room and cost of painting 1 sq ft from user.
Q.10 How is '/' operator different from '%'? Explain with example.

Compiled By : Angel Deepak Godson Page 8|8

You might also like