computer programming lecture – 17 to 19

31
Lecture – 17 to 19 Computer Programming 14 Computer Systems Engineering – Second Semester By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

Upload: ali-asghar-manjotho

Post on 09-Jul-2015

107 views

Category:

Education


6 download

DESCRIPTION

Computer programming lecture – 17 to 19

TRANSCRIPT

Page 1: Computer programming lecture – 17 to 19

Lecture – 17 to 19Computer Programming

14 Computer Systems Engineering – Second Semester

By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

Page 2: Computer programming lecture – 17 to 19

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

Page 3: Computer programming lecture – 17 to 19

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

Page 4: Computer programming lecture – 17 to 19

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

Page 5: Computer programming lecture – 17 to 19

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

Page 6: Computer programming lecture – 17 to 19

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

Page 7: Computer programming lecture – 17 to 19

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

Page 8: Computer programming lecture – 17 to 19

Arithmetic Operators in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 8

Page 9: Computer programming lecture – 17 to 19

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

Page 10: Computer programming lecture – 17 to 19

Assignment Operator in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 10

Page 11: Computer programming lecture – 17 to 19

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

Page 12: Computer programming lecture – 17 to 19

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

Page 13: Computer programming lecture – 17 to 19

Arithmetic Assignment Operators in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 13

Page 14: Computer programming lecture – 17 to 19

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

Page 15: Computer programming lecture – 17 to 19

Relational Operators in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 15

Page 16: Computer programming lecture – 17 to 19

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

Page 17: Computer programming lecture – 17 to 19

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

Page 18: Computer programming lecture – 17 to 19

Logical Operators in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 18

Page 19: Computer programming lecture – 17 to 19

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

Page 20: Computer programming lecture – 17 to 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

Page 21: Computer programming lecture – 17 to 19

Increment & Decrement Operators in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 21

Page 22: Computer programming lecture – 17 to 19

Program ExamplesLogic circuit diagrams and logical operators in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 22

Page 23: Computer programming lecture – 17 to 19

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

Page 24: Computer programming lecture – 17 to 19

Program Example 01

Ali Asghar Manjotho, Lecturer CSE-MUET 24

Page 25: Computer programming lecture – 17 to 19

Program Example 01

Ali Asghar Manjotho, Lecturer CSE-MUET 25

Page 26: Computer programming lecture – 17 to 19

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

Page 27: Computer programming lecture – 17 to 19

Program Example 02

Ali Asghar Manjotho, Lecturer CSE-MUET 27

Page 28: Computer programming lecture – 17 to 19

Program Example 02

Ali Asghar Manjotho, Lecturer CSE-MUET 28

Page 29: Computer programming lecture – 17 to 19

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

Page 30: Computer programming lecture – 17 to 19

Program Example 03

Ali Asghar Manjotho, Lecturer CSE-MUET 30

Page 31: Computer programming lecture – 17 to 19

Program Example 03

Ali Asghar Manjotho, Lecturer CSE-MUET 31