0% found this document useful (0 votes)
19 views38 pages

CP Lecture 04

Introduction in cp
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)
19 views38 pages

CP Lecture 04

Introduction in cp
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/ 38

CP 111:

PRINCIPLES OF
PROGRAMMING
Operators
CONTENTS
 Operators
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Increment and Decrement Operators
OPERATORS
 An operator is a symbol, which helps the user to
command the computer to do a certain mathematical
or logical manipulations.
 Operators are used in programming language
program to operate on data and variables.
OPERATORS
 Operators can be classified as:
– Arithmetic operators
– Relational Operators
– Logical Operators
– Increments and Decrement Operators
-Assignment operators
ARITHMETIC OPERATORS
 You can use an arithmetic operator with one or two arguments to add,
subtract, multiply, and divide numeric values.
ARITHMETIC OPERATORS

 Example:
i. 5+3=8
ii. 5–3=2
iii. 5 * 3 = 15
iv. 5 / 3 = 1
v. 5 % 3 = 2
ARITHMETIC OPERATORS

 *, / and % will be performed before + or - in any


expression.
 Brackets can be used to force a different order of
evaluation to this.
 Example
 2+5*4–3=?
 (2 + 5) * (4 – 3) =
ARITHMETIC OPERATORS

 Here are some arithmetic expressions used within


assignment statements:
i. z=x+y
ii. no1 = x – y
iii. age = a * b + c
iv. velocity = distance / time
v. force = mass * acceleration
vi. count = count + 1
ARITHMETIC OPERATORS
 Integer Arithmetic
 When an arithmetic operation is performed on
two whole numbers or integers than such an
operation is called as integer arithmetic.
It always gives an integer as the result.
ARITHMETIC OPERATORS
 Example
 Let x = 27 and y = 5 be two integer numbers. Then
the integer operation leads to the following results:
i. x + y = 32
ii. x – y = 22
iii. x * y = 115
iv. x % y = 2
v. x/y=5
ARITHMETIC OPERATORS
 Floating-point Arithmetic
 When an arithmetic operation is preformed on
two real numbers or fraction numbers such an
operation is called floating-point arithmetic.
ARITHMETIC OPERATORS
 Example
 Let x = 14.0 and y = 4.0 then
i. x + y = 18.0
ii. x – y = 10.0
iii. x * y = 56.0
iv. x / y = 3.50
RELATIONAL OPERATORS
 An operator that compares two values.
 For example, the expression:

 This expression will have a value of TRUE if the variable x is


less than 5;
 otherwise the value of the expression will be FALSE.
RELATIONAL OPERATORS
 Relational operators are sometimes called comparison operators.
 Expressions that contain relational operators are called relational
expressions.
 A simple relational expression contains only one relational operator and
takes the following form:

 Where exp1 and exp2 are expressions, which may be simple constants,
variables or combination of them.
RELATIONAL OPERATORS
 The following are relational operators
RELATIONAL OPERATORS
 The following are relational operators
RELATIONAL OPERATORS
Example:
Let x = 2 and y = 5 then
i. x < y = True
ii. (x + 2) > (y * 2) = False
iii. (x + 3) <= y = True
iv. x != y = True
v. y > (3 + x) = False
LOGICAL OPERATORS
 An operator that compare or evaluate logical and relational expressions.
 The following are logical operators:
LOGICAL OPERATORS
 Logical AND
 This operator is used to evaluate two conditions or
expressions with relational operators simultaneously.
 If both the expressions to the left and to the right of
the logical operator is true then the whole compound
expression is true.
LOGICAL OPERATORS
Logical AND
LOGICAL OPERATORS
 Logical AND
 Example:
 (a > b) && (x == 10)
 The expression to the left is a > b and that on the
right is x == 10, the whole expression is true only if
both expressions are true i.e., if a is greater than b
and x is equal to 10.
LOGICAL OPERATORS
 Logical AND
 Example:
 Given a = 2, b = 3 and c = 5, evaluate the following
logical expressions:
 i. (a > b) && (c != 5) = False
 ii. (a < b) && (c < b) = False
 iii. (a > b) && (c == 5) = False
 iv. (a < b) && (b < c) = True
LOGICAL OPERATORS
Logical OR
 The logical OR is used to combine two
expressions or the condition evaluates to true
if any one of the 2 expressions is true.
 The expression evaluates to true if any one of
them is true or if both of them are true.
LOGICAL OPERATORS
Logical OR
LOGICAL OPERATORS
Logical OR
 Example:
 (a < m) || (a < n)
 The expression evaluates to true if any one of them is
true or if both of them are true.
LOGICAL OPERATORS
Logical OR
 Example:
 Given a = 2, b = 3 and c = 5, evaluate the following
logical expressions:
 i. (a > b) || (c != 5) = False
 ii. (a < b) || (c < b) = True
 iii. (a > b) || (c == 5) = True
 iv. (a < b) || (b < c) = True
LOGICAL OPERATORS
Logical NOT
• The logical NOT operator takes single expression and
evaluates to true if the expression is false and
evaluates to false if the expression is true.
• In other words it just reverses the value of the
expression.
LOGICAL OPERATORS
Logical NOT
Example:
! (x >= y)
The NOT expression evaluates to true only if the value
of x is neither greater than or equal to y
LOGICAL OPERATORS
Logical NOT
Example:
Given a = 2, b = 3 and c = 5, evaluate the following
logical expressions:
i) !(a > b) = True
ii) !(a < b) = False
iii) !(a > b || c == 5) = False
LOGICAL OPERATORS
Increment and Decrement Operators
• The increment and decrement operators are one of
the unary operators which are very useful in
programming language.
• They are extensively used in loops.
• The syntax of the operators is given below:
++ variable name, variable name++
– –variable name, variable name– –
INCREMENT AND
DECREMENT OPERATORS
Increment and Decrement Operators
• The increment operator ++ adds the value 1
to the current value of operand.
• The decrement operator – – subtracts the
value 1 from the current value of operand.
INCREMENT AND
DECREMENT OPERATORS
INCREMENT AND
DECREMENT OPERATORS
 Increment and Decrement Operators
 A prefix operator first adds 1 to the operand
and then the result is assigned to the variable
on the left.
 On the other hand, a postfix operator first
assigns the value to the variable on the left
and then increments the operand.
INCREMENT AND
DECREMENT OPERATORS
 Increment and Decrement Operators
 Example 1:
x = 4 y = ++x PRINT x , PRINT y
 What is the output?
 5
 5
INCREMENT AND
DECREMENT OPERATORS
 Increment and Decrement Operators
 Example 2:
x = 3 y = x++ PRINT x, PRINT y
 What is the output?
3
4
INCREMENT AND
DECREMENT OPERATORS
 Increment and Decrement Operators
 Example 3:
x = 3 y = --x PRINT x , PRINT y
 What is the output?
 2
 2
INCREMENT AND
DECREMENT OPERATORS
 Increment and Decrement Operators
 Example 4:
x = 3 y = x- - PRINT x,
PRINT y
 What is the output?
3
 2
END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy