You are on page 1of 31

Lecture – 13

Introduction To Computing & Programming


Contents
• Operators and Operands
• Types of Operators
• Categories of Operators in C++
• Arithmetic Operators in C++
• Assignment Operator in C++
• Arithmetic Assignment Operators in C++
• Relational Operators in C++
• Logical Operators in C++
• Increment and Decrement Operators in C++
• Problem Examples

2
Operators and Operands

• Almost all of the programs use some sort of data stored in variables.
• In a program different types of operations are performed on that data.
• The data on which operations are performed are known as operands and the types
of the operations performed are known as operators.
• For Example:

A+B Operator

Operands

3
Types of Operators

Unary Operators
• Perform operation on one operand
Binary Operators
• Perform operation on two operands
Ternary Operators
• Perform operation on three operands

4
Categories of Operators in C++
• Arithmetic Operators ( + , - , / , * , %)
• Assignment Operator ( = )
• Arithmetic Assignment Operators ( += , -= , /= , *= , %=)
• Relational Operators ( > , < , >= , <= , == , !=)
• Logical Operators ( && , | | , ! )
• Increment and Decrement Operators ( ++ , - - )
• Bitwise Operators ( & , | , ~ , ^ , >> , << )
• Conditional Operator ( ? : )
5
Arithmetic Operators in C++
• They perform arithmetic operations on the program data.

Operation Description Type Example


+ Addition Binary a+b

- Subtraction Binary a-b

/ Division Binary a/b

* Multiplication Binary a*b

% Modulus / Remainder Binary a%b

6
Arithmetic Operators in C++

• Addition, Subtraction, Multiplication and Division operations can be


performed on integer numbers as well as floating point numbers.

• Remainder operation can only be performed on integer numbers.

• Floating point operands are not accepted by remainder operator.

7
Arithmetic Operators in C++

8
Assignment Operator in C++
• Assignment operator assigns a constant value, a variable or equation to a single
variable.
• There is one assignment operator ( = ). It is binary operator.
• It assigns anything on its right side to its left side.

float radius = 6.98 ; a = b; c = ( f – 32 ) / 1.8 ;

= Assignment Operator
9
Assignment Operator in C++

10
Arithmetic Assignment Operators in C++
• They perform arithmetic operations on two operands and assigns the resultant in
the same first operand.

Operation Description Type Example Same as


+= Addition Assignment Binary a += b a=a+b

-= Subtraction Assignment Binary a -= b a=a-b

/= Division Assignment Binary a /= b a=a/b

*= Multiplication Assignment Binary a *= b a=a*b


Modulus / Remainder
%= Assignment
Binary a %= b a=a%b

11
Arithmetic Assignment Operators in C++
a += b is same as a = a+b

a = a+b
First operand = a
Second operand = b
Result stored back in to first operand i.e. a

12
Arithmetic Assignment Operators in C++

13
Relational Operators in C++
• They compare two operands and return 0 or 1. If comparison is successful, they
return 1 else return 0. They are used to create conditions. One relational operator
creates one condition.
Operation Description Type Example
> Greater Than Binary a>b

< Less Than Binary a<b

>= Greater Than or Equals To Binary a >= b

<= Less Than or Equals To Binary a <= b

== Equals To Binary a == b

!= Not Equals To Binary a != b


14
Relational Operators in C++

15
Logical Operators in C++
• They perform logical operations on the logical operands. They return either 0 or 1.
• They combine multiple conditions to form one composite condition.

Operation Description Type Example


&& Logical AND Binary a && b

|| Logical OR Binary a||b

! Logical NOT Unary !a

16
Logical Operators in C++

a b a && b a b a||b
0 0 0 a !a 0 0 0
0 1 0 0 1 0 1 1
1 0 0 1 0 1 0 1
1 1 1 1 1 1

17
Logical Operators in C++

18
Logical Operators in C++
• Logical AND is also called as Short Circuit AND operator.
If the first operand is false ( 0 ) then it will not check the second operand.
• Logical OR is also called as Short Circuit OR operator.
If the first operand is true ( 1 ) then it will not check the second operand.

int num1 = 5, num2 = 10, num3 = 6, num4 = 15 ;


This will not be checked
cout<< num1 > num2 && num3 < num4 ;
This will not be checked
cout<< num1 < num2 | | num3 != num4 ;
19
Increment & Decrement Operators in C++
• They are unary operators, who require only one operand.
• They increment or decrement the value of operand by one.
• There are two versions of operators: Prefix and Postfix.

Operation Description Type Example Operation

++ Prefix Increment Unary ++a Increments a, then evaluates a

-- Prefix Decrement Unary --a Decrements a, then evaluates a

++ Postfix Increment Unary a++ Evaluates a, then increments a

-- Postfix Decrement Unary a-- Evaluates a, then decrements a

20
Increment & Decrement Operators in C++

21
Program Examples
Logic circuit diagrams and logical operators in C++

22
Program Example 01
Problem Statement:
Write a logic circuit solver program in C++ that solves the following logic
circuit. Where all the inputs are logic switches (which can be either Turned ON
(True, 1) or Turned OFF (False, 0)). The program should display the output
value.

23
Program Example 01

24
Program Example 01

25
Program Example 02
Problem Statement:
Write a logic circuit solver program in C++ that solves the following logic
circuit. Where all the inputs are logic switches (which can be either Turned ON
(True, 1) or Turned OFF (False, 0)). The program should display the output
value.

26
Program Example 02

27
Program Example 02

28
Program Example 03
Problem Statement:
Write a logic circuit solver program in C++ that accepts all the inputs from the
user and displays the output of the following circuit. Where all the inputs are
logic switches (which can be either Turned ON (True, 1) or Turned OFF (False,
0)).

29
Program Example 03

30
Program Example 03

31

You might also like