computer programming lecture – 17 to 19
DESCRIPTION
Computer programming lecture – 17 to 19TRANSCRIPT
Lecture – 17 to 19Computer Programming
14 Computer Systems Engineering – Second Semester
By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET
Contents
• Operators and Operands (LL 02)
• Types of Operators (LL 02)
• Categories of Operators in C++ (LL 02)
• Arithmetic Operators in C++ (LL 04)
• Assignment Operator in C++ (LL 04)
• Arithmetic Assignment Operators in C++ (LL 04)
• Relational Operators in C++ (LL 04)
• Logical Operators in C++ (LL 04)
• Increment and Decrement Operators in C++ (LL 04)
• Problem Examples (LL 04)LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis
Ali Asghar Manjotho, Lecturer CSE-MUET 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
Ali Asghar Manjotho, Lecturer CSE-MUET 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
Ali Asghar Manjotho, Lecturer CSE-MUET 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 ( ? : )
Ali Asghar Manjotho, Lecturer CSE-MUET 5
Arithmetic Operators in C++
• They perform arithmetic operations on the program data.
Ali Asghar Manjotho, Lecturer CSE-MUET 6
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
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.
Ali Asghar Manjotho, Lecturer CSE-MUET 7
Arithmetic Operators in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 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 OperatorAli Asghar Manjotho, Lecturer CSE-MUET 9
Assignment Operator in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 10
Arithmetic Assignment Operators in C++• They perform arithmetic operations on two operands and assigns the resultant in
the same first operand.
Ali Asghar Manjotho, Lecturer CSE-MUET 11
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
AssignmentBinary a %= b a = a % b
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
Ali Asghar Manjotho, Lecturer CSE-MUET 12
Arithmetic Assignment Operators in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 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.
Ali Asghar Manjotho, Lecturer CSE-MUET 14
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
Relational Operators in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 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.
Ali Asghar Manjotho, Lecturer CSE-MUET 16
Operation Description Type Example
&& Logical AND Binary a && b
| | Logical OR Binary a | | b
! Logical NOT Unary ! a
Logical Operators in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 17
a b a && b
0 0 0
0 1 0
1 0 0
1 1 1
a b a | | b
0 0 0
0 1 1
1 0 1
1 1 1
a ! a
0 1
1 0
Logical Operators in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 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 ;
Ali Asghar Manjotho, Lecturer CSE-MUET 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.
Ali Asghar Manjotho, Lecturer CSE-MUET 20
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
Increment & Decrement Operators in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 21
Program ExamplesLogic circuit diagrams and logical operators in C++
Ali Asghar Manjotho, Lecturer CSE-MUET 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.
Ali Asghar Manjotho, Lecturer CSE-MUET 23
Program Example 01
Ali Asghar Manjotho, Lecturer CSE-MUET 24
Program Example 01
Ali Asghar Manjotho, Lecturer CSE-MUET 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.
Ali Asghar Manjotho, Lecturer CSE-MUET 26
Program Example 02
Ali Asghar Manjotho, Lecturer CSE-MUET 27
Program Example 02
Ali Asghar Manjotho, Lecturer CSE-MUET 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)).
Ali Asghar Manjotho, Lecturer CSE-MUET 29
Program Example 03
Ali Asghar Manjotho, Lecturer CSE-MUET 30
Program Example 03
Ali Asghar Manjotho, Lecturer CSE-MUET 31