Professional Documents
Culture Documents
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.
6
Arithmetic Operators in C++
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.
= 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.
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
== Equals To Binary a == b
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.
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.
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