0% found this document useful (0 votes)
2 views21 pages

Lecture - 15-16

The document covers programming fundamentals focusing on operators and operands in C++. It details various types of operators including arithmetic, assignment, relational, logical, and increment/decrement operators, along with their functions and examples. The content is aimed at first-semester BS Information Technology students and is presented by Ms. Mahnoor Buriro.

Uploaded by

fayazahmex561
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Lecture - 15-16

The document covers programming fundamentals focusing on operators and operands in C++. It details various types of operators including arithmetic, assignment, relational, logical, and increment/decrement operators, along with their functions and examples. The content is aimed at first-semester BS Information Technology students and is presented by Ms. Mahnoor Buriro.

Uploaded by

fayazahmex561
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Lecture – 17, 18 & 19

Programming Fundamentals
BS Information Technology –
First Semester

By: Ms. Mahnoor Buriro, Research Associate,


TUMS
Content

s
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

4
Categories of Operators in
C++
• Arithmetic Operators ( + , - , / , * , %)
• Assignment Operator ( = )
• Arithmetic Assignment Operators ( += , -= , /= , *= ,
• Relational Operators ( > , < , >= , <= , == %=)
, !=)
• Logical Operators ( && , | | , ! )
• Increment and Decrement Operators ( ++ , -
-)

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 = a = c = ( f – 32 ) /
6.98 ; b; 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+
First operand = a
b
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 Example
> Greater Than a>b

< Less Than a<b

>= Greater Than or Equals To a >= b

<= Less Than or Equals To a <= b

== Equals To a == b

!= Not Equals To 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
cout<< num1 > num2 num3 < checked
&& num4 ; This will not be
cout<< num1 < | | num3 != checked
num2 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
Operation Description Type Example Operation
Postfix.
++ 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

You might also like