SlideShare a Scribd company logo
Balochistan University of IT & MS 1
Lecture 4
Operators and Expressions
CS1
Introduction to Programming
Methodologies & Abstraction
Fall 2005
Balochistan University
of I.T & M.S
Faculty of System Sciences
Sadique Ahmed Bugti
Balochistan University of IT & MS 2
Expressions
• Expressions are meaningful combinations
of constants, variables and function calls.
• Most expressions have both a value and a
type.
• The expression may consist of a single
entity, such as a constant or variable, or it
may consist of some combination of such
entities, interconnected by one or more
operators.
• Expressions can also represent logical
conditions which are either true or false.
Balochistan University of IT & MS 3
Example Expressions
• Several simple expressions are given below:
a + b
– This expression represents the sum of the
values assigned to variables a and b.
x = y
– This expression causes the value
represented by y to be assigned to x.
Balochistan University of IT & MS 4
Example Expressions
t = u + v
– In this expression, the value of the
expression (u + v) is assigned to t.
It is poor programming practice to mix
data types in assignment expressions.
Thus, the data types of the constants or
variables on either side of the = sign
should always match.
Balochistan University of IT & MS 5
Expression Statements
• An expression statement consists of an
expression followed by a semicolon.
• For example:
area = PI * radius * radius;
– This causes the value of the
expression on the right of the equal
sign to be assigned to the variable on
the left.
Balochistan University of IT & MS 6
Assignment Statements
• In an assignment statement, the value
of a variable is replaced with the value
of an expression.
• e.g. PI = 3.14159;
• The general form of the assignment
statement is:
variable = expression;
• The = operator is used for assignment.
Balochistan University of IT & MS 7
Assignment Statements
• Even though assignment statements sometimes
resemble mathematical equations, the two
notions are distinct and should not be confused.
• The mathematical equation:
χ + 2 = 0
does not become an assignment statement
when you type:
x + 2 = 0;
• The left side of the equal sign is an expression,
not a variable, and this expression may not be
assigned a value.
Balochistan University of IT & MS 8
Defining Equations
• The following example is the equation
of a straight line:
Algebra: y = mx + c
C: y = m * x + c
Algebra: a = πr2
C: area = π * r * r
Balochistan University of IT & MS 9
Defining Equations
X1 = (-b+ sqrt((b * b) – 4 * a * c )) )/ (2 *a)
X2 = (-b – sqrt((b * b) – 4 * a * c))) / (2 * a)
Balochistan University of IT & MS 10
What are operators?
• C has a rich set of operators (i.e., things like +
- * / ), which allow you to write complicated
expressions quite compactly.
• General expressions are formed by joining
together constants and variables (operands)
via various operators.
• Operators in C fall into a number of classes:
– arithmetic operators, unary operators,
relational and logical operators, assignment
operators,equality operators, and the
conditional operator.
Balochistan University of IT & MS 11
Unary Operators
• Unary operators are operators that only
take one argument.
+123 positive 123
-123 negative 123
!i logical negation (i.e., 1 if i is zero, 0 otherwise)
++i adds one to i, and returns the new value of i
– –i subtracts one from i, and returns the new value of i
i++ adds one to i, and returns the old value of i
i– – subtracts one from i, and returns the old value of i
Balochistan University of IT & MS 12
Unary –
• The most common unary operator is the
unary minus, which occurs when a
numerical constant, variable, or
expression is preceded by a minus sign.
• Note that the unary minus is distinctly
different from the arithmetic operator (–)
which denotes subtraction, since the latter
operator acts on two separate operands.
e.g. -12
Balochistan University of IT & MS 13
Binary Operators
• Binary operators work on two
operands ('binary‘ here means 2
operands, not in the sense of base-2
arithmetic).
Balochistan University of IT & MS 14
Binary Operators
+ addition
– subtraction
* multiplication
/ division
% remainder (e.g., 2%3 is 2), also called 'modulo'
<< left-shift (e.g., i<<j is i shifted to the left by j bits)
>> right-shift
& bit wise AND
| bit wise OR
^ bit wise exclusive-OR
Balochistan University of IT & MS 15
Binary Operators
&& logical AND (returns 1 if both operands are
non-zero; else 0)
|| logical OR (returns 1 if either operand is non-
zero; else 0)
< less than (e.g., i<j returns 1 if i is less than j)
> greater than
<= less than or equal
>= greater than or equal
== equals
!= does not equal
? conditional operator
Balochistan University of IT & MS 16
Assignment Operators
• The most common assignment
operator in C is the equals operator, =.
– It is used to change the value of a variable.
• For instance, the expression:
f = 3.4;
• causes the floating-point value 3.4 to
be assigned to the variable f.
Balochistan University of IT & MS 17
Assignment Operators
• Multiple assignments are permissible in
C. For example,
i = j = k = 4;
• causes the integer value 4 to be
assigned to i, j, and k, simultaneously.
Balochistan University of IT & MS 18
Assignment Operators
• Assignment operators are really just binary
operators.
= assignment
+= addition assignment
-= subtraction assignment
*= multiplication assignment
/= division assignment
%= remainder/modulus assignment
&= bit wise AND assignment
|= bit wise OR assignment
^= bit wise exclusive OR assignment
<<= left shift assignment
>>= right shift assignment
Balochistan University of IT & MS 19
Assignment Operators
• Examples of using assignment
operators:
velocity = distance / time;
force = mass * acceleration;
count = count + 1;
Balochistan University of IT & MS 20
Arithmetic Operators
• There are four main arithmetic operators in C:
addition +
subtraction –
multiplication *
division /
• There is no built-in exponentiation operator in
C . Instead, there is a library function (pow)
which carries out this operation.
e.g. x2 is represented as x * x
Balochistan University of IT & MS 21
Division, /
• The division operator is special.
– There is a vast difference between an
integer divide and a floating-point divide
– In an integer divide, the result is truncated
i.e. the fractional part is discarded
– If either the divisor or the dividend is a
floating-point number, a floating-point
divide is performed.
– e.g. 19/10 = 1
– 19.0/10.0 = 1.9
Balochistan University of IT & MS 22
Integer Division, /
• Integer division returns just the result
with no remainder.
• For example:
15/3 = 5 16/3 = 5 17/3 = 5
3/15 = 0 0/4 = 0 4/0 = undefined
Balochistan University of IT & MS 23
Arithmetic Operators
Operation C Operator Algebraic
Expression
C Expression
Addition + f + 7 f + 7
Subtraction – a – b a - b
Multiplication * bm b * m
Division / x / y or x ÷ y x / y
Modulus % r mod s r % s
Balochistan University of IT & MS 24
Parentheses, ( )
• Parentheses are used as punctuators
to clarify or change the order in which
operations are performed.
• For example:
a = 12 * (x + y)
Balochistan University of IT & MS 25
Note
• An alternative to turning one statement into two is to
split long statements into multiple lines.
– When breaking up a line, the preferred split point is where
the parenthetic nesting is lowest.
result = ( ( ( x + 1 ) * ( x + 1 )) - ( ( y + 1 ) * ( y + 1 ) ) );
1233333332223333333211123333333222333333321
• The best place to break the line is where the nesting
level is lowest; in this case at the – operator in the
middle:
result =(((x1 + 1) * (x1 + 1))
-((y1 + 1) * (y1 + 1)));
Balochistan University of IT & MS 26
Operator Precedence
• The operators within C are grouped
hierarchically according to their precedence
(i.e., their order of evaluation).
• Amongst the arithmetic operators, * and /
have precedence over + and –.
– In other words, when evaluating
expressions, C performs multiplication
and division operations prior to addition
and subtraction operations.
• The rules of precedence can always be
bypassed by the use of parentheses, ( , ).
Balochistan University of IT & MS 27
Operator Precedence
• For example:
a - b / c + d
is equivalent to the unambiguous
expression
a - (b / c) + d
• since division takes precedence over
addition and subtraction.
Balochistan University of IT & MS 28
Operator Precedence and
Associatively
• The precedence of an operator gives the
order in which operators are applied in
expressions: the highest precedence
operator is applied first, followed by the next
highest, and so on.
• The associatively of an operator gives the
order in which expressions involving
operators of the same precedence are
evaluated.
Balochistan University of IT & MS 29
Operator Precedence and
Associatively
Balochistan University of IT & MS 30
Operator Precedence and
Associatively
• Note: the +, – and * operators appear twice
in the above table. The unary forms (on the
second line) have higher precedence that
the binary forms.
• Operators on the same line have the same
precedence, and are evaluated in the order
given by the associatively.
• To specify a different order of evaluation
you can use parentheses. In fact, it is often
good practice to use parentheses to guard
against making mistakes in difficult cases,
or to make your meaning clear.
Balochistan University of IT & MS 31
Exercise
Evaluate the value of following expressions
m = 3, n=4
X = 2.5, y= 10
1. Z= m + n + x + y
2. Z= m + x * n + y
3. Z= X / y + m / n
4. Z= X – y * m + y / n
5. Z= X / 0
Balochistan University of IT & MS 32
Mathematical Operators For
Increment / Decrement
k
k :=k+1
k 4
3 k 9
K:= k-1
k 8
Balochistan University of IT & MS 33
Exercise
command x y z
x = 2 2 unknown unknown
x = x + 1 3 unknown unknown
y = 2 3 2 unknown
z = x + y 3 2 5
y = x + z 3 8 5
z = z - 2 3 8 3
x = 3 + z 6 8 3
x = y mod z 2 8 3
Balochistan University of IT & MS 34
Relational Operators
Symbol Operator Example
< Less Than a < b
> Greater Than a > b
= Equal to a = b
<= Less or Equal a <= b
>= Greater or
Equal
a >=b
<> Not Equal a <> b
Balochistan University of IT & MS 35
Relational Operators
a = 2, b=5
Symbol Operator OutPut
< a < b 1
> a > b 0
= a = b 0
<= a <= b 1
>= a >=b 0
<> a <> b 1
Balochistan University of IT & MS 36
Logical Operators
Effects on relational expression’s results also AND, OR combines
two or more Relational expressions, Results will be returned
either TRUE( 1 ) or FALSE ( 0 ) regards with specific situations.
Symbol Operator Example
AND AND A< b AND c > d
OR OR a < b OR c > d
NOT NOT NOT ( a < b )
Balochistan University of IT & MS 37
Logical Operators
a = 2, b = 5, c = 9, d = 7
Symbol Operator Example
AND a< b AND c > d 1
OR a < b OR c > d 1
NOT NOT ( a < b ) 0
Balochistan University of IT & MS 38
Exercise
Before Execution Expression After execution
a = 2 , b = -1 a = a + b a=
a = 2 a = -a a=
x = 4 , y := 3 z = x > y + 1 z = , x =, y =
a = 1 z = a != a z =
x = 11, y = 6 z = x > 9 && y != 3 z =
x = 11, y = 6 z = x = 5 || y != 3 z =
x = 11, y = 6 z = ! ( x > 14 ) z=
x = 11, y = 6 z = !( x > 9 AND y !=23) z=
a = 13 , b = 4 z = a % b z=
a = 3, x = a z = x = a; z= , x= , a=

More Related Content

What's hot

Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational Calculus
Kunal Anand
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
Vignesh Saravanan
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
uoitc
 
Relational Algebra Introduction
Relational Algebra IntroductionRelational Algebra Introduction
Relational Algebra Introduction
Md. Afif Al Mamun
 
Relational Algebra-Database Systems
Relational Algebra-Database SystemsRelational Algebra-Database Systems
Relational Algebra-Database Systemsjakodongo
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)yourbookworldanil
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculusKumar
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lectures
marwaeng
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2Mukund Trivedi
 
Functions in discrete mathematics
Functions in discrete mathematicsFunctions in discrete mathematics
Functions in discrete mathematics
Rachana Pathak
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
Edward Blurock
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptx
RUpaliLohar
 
1643 y є r relational calculus-1
1643 y є r  relational calculus-11643 y є r  relational calculus-1
1643 y є r relational calculus-1
Dr Fereidoun Dejahang
 
Linear functions and modeling
Linear functions and modelingLinear functions and modeling
Linear functions and modeling
IVY SOLIS
 
Alg II 2-3 and 2-4 Linear Functions
Alg II 2-3 and 2-4 Linear FunctionsAlg II 2-3 and 2-4 Linear Functions
Alg II 2-3 and 2-4 Linear Functionsjtentinger
 
A review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementationA review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementation
ssuserfa7e73
 
Master of Computer Application (MCA) – Semester 4 MC0079
Master of Computer Application (MCA) – Semester 4  MC0079Master of Computer Application (MCA) – Semester 4  MC0079
Master of Computer Application (MCA) – Semester 4 MC0079
Aravind NC
 

What's hot (19)

Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational Calculus
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
 
Relational Algebra Introduction
Relational Algebra IntroductionRelational Algebra Introduction
Relational Algebra Introduction
 
Relational Algebra-Database Systems
Relational Algebra-Database SystemsRelational Algebra-Database Systems
Relational Algebra-Database Systems
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)
 
Relational+algebra (1)
Relational+algebra (1)Relational+algebra (1)
Relational+algebra (1)
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculus
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lectures
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2
 
Functions in discrete mathematics
Functions in discrete mathematicsFunctions in discrete mathematics
Functions in discrete mathematics
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptx
 
1643 y є r relational calculus-1
1643 y є r  relational calculus-11643 y є r  relational calculus-1
1643 y є r relational calculus-1
 
Linear functions and modeling
Linear functions and modelingLinear functions and modeling
Linear functions and modeling
 
Alg II 2-3 and 2-4 Linear Functions
Alg II 2-3 and 2-4 Linear FunctionsAlg II 2-3 and 2-4 Linear Functions
Alg II 2-3 and 2-4 Linear Functions
 
A review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementationA review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementation
 
Master of Computer Application (MCA) – Semester 4 MC0079
Master of Computer Application (MCA) – Semester 4  MC0079Master of Computer Application (MCA) – Semester 4  MC0079
Master of Computer Application (MCA) – Semester 4 MC0079
 

Similar to Operators in C

Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in C
yarkhosh
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
rinkugupta37
 
python ppt
python pptpython ppt
python ppt
EmmanuelMMathew
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
Online
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
SKUP1
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
LECO9
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressionsvishaljot_kaur
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...
Fernando Loizides
 
operator
operatoroperator
operator
aamirsahito
 
Coper in C
Coper in CCoper in C
Coper in C
thirumalaikumar3
 
5_Operators.pdf
5_Operators.pdf5_Operators.pdf
5_Operators.pdf
NiraliArora2
 
ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptx
AvijitChaudhuri3
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
Mohammad Imam Hossain
 
This slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptxThis slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptx
ranaashutosh531pvt
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operatorsdishti7
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
Praveen M Jigajinni
 

Similar to Operators in C (20)

Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in C
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
python ppt
python pptpython ppt
python ppt
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...
 
operator
operatoroperator
operator
 
Coper in C
Coper in CCoper in C
Coper in C
 
5_Operators.pdf
5_Operators.pdf5_Operators.pdf
5_Operators.pdf
 
Order of Operations
Order of OperationsOrder of Operations
Order of Operations
 
ESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptxESCM303 Introduction to Python Programming.pptx
ESCM303 Introduction to Python Programming.pptx
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
This slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptxThis slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptx
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 

More from yarkhosh

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
yarkhosh
 
Algorithm
AlgorithmAlgorithm
Algorithm
yarkhosh
 
Math Functions in C Scanf Printf
Math Functions in C Scanf PrintfMath Functions in C Scanf Printf
Math Functions in C Scanf Printf
yarkhosh
 
Data Types in C
Data Types in CData Types in C
Data Types in C
yarkhosh
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
yarkhosh
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programming
yarkhosh
 

More from yarkhosh (6)

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Math Functions in C Scanf Printf
Math Functions in C Scanf PrintfMath Functions in C Scanf Printf
Math Functions in C Scanf Printf
 
Data Types in C
Data Types in CData Types in C
Data Types in C
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programming
 

Recently uploaded

Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 

Recently uploaded (20)

Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 

Operators in C

  • 1. Balochistan University of IT & MS 1 Lecture 4 Operators and Expressions CS1 Introduction to Programming Methodologies & Abstraction Fall 2005 Balochistan University of I.T & M.S Faculty of System Sciences Sadique Ahmed Bugti
  • 2. Balochistan University of IT & MS 2 Expressions • Expressions are meaningful combinations of constants, variables and function calls. • Most expressions have both a value and a type. • The expression may consist of a single entity, such as a constant or variable, or it may consist of some combination of such entities, interconnected by one or more operators. • Expressions can also represent logical conditions which are either true or false.
  • 3. Balochistan University of IT & MS 3 Example Expressions • Several simple expressions are given below: a + b – This expression represents the sum of the values assigned to variables a and b. x = y – This expression causes the value represented by y to be assigned to x.
  • 4. Balochistan University of IT & MS 4 Example Expressions t = u + v – In this expression, the value of the expression (u + v) is assigned to t. It is poor programming practice to mix data types in assignment expressions. Thus, the data types of the constants or variables on either side of the = sign should always match.
  • 5. Balochistan University of IT & MS 5 Expression Statements • An expression statement consists of an expression followed by a semicolon. • For example: area = PI * radius * radius; – This causes the value of the expression on the right of the equal sign to be assigned to the variable on the left.
  • 6. Balochistan University of IT & MS 6 Assignment Statements • In an assignment statement, the value of a variable is replaced with the value of an expression. • e.g. PI = 3.14159; • The general form of the assignment statement is: variable = expression; • The = operator is used for assignment.
  • 7. Balochistan University of IT & MS 7 Assignment Statements • Even though assignment statements sometimes resemble mathematical equations, the two notions are distinct and should not be confused. • The mathematical equation: χ + 2 = 0 does not become an assignment statement when you type: x + 2 = 0; • The left side of the equal sign is an expression, not a variable, and this expression may not be assigned a value.
  • 8. Balochistan University of IT & MS 8 Defining Equations • The following example is the equation of a straight line: Algebra: y = mx + c C: y = m * x + c Algebra: a = πr2 C: area = π * r * r
  • 9. Balochistan University of IT & MS 9 Defining Equations X1 = (-b+ sqrt((b * b) – 4 * a * c )) )/ (2 *a) X2 = (-b – sqrt((b * b) – 4 * a * c))) / (2 * a)
  • 10. Balochistan University of IT & MS 10 What are operators? • C has a rich set of operators (i.e., things like + - * / ), which allow you to write complicated expressions quite compactly. • General expressions are formed by joining together constants and variables (operands) via various operators. • Operators in C fall into a number of classes: – arithmetic operators, unary operators, relational and logical operators, assignment operators,equality operators, and the conditional operator.
  • 11. Balochistan University of IT & MS 11 Unary Operators • Unary operators are operators that only take one argument. +123 positive 123 -123 negative 123 !i logical negation (i.e., 1 if i is zero, 0 otherwise) ++i adds one to i, and returns the new value of i – –i subtracts one from i, and returns the new value of i i++ adds one to i, and returns the old value of i i– – subtracts one from i, and returns the old value of i
  • 12. Balochistan University of IT & MS 12 Unary – • The most common unary operator is the unary minus, which occurs when a numerical constant, variable, or expression is preceded by a minus sign. • Note that the unary minus is distinctly different from the arithmetic operator (–) which denotes subtraction, since the latter operator acts on two separate operands. e.g. -12
  • 13. Balochistan University of IT & MS 13 Binary Operators • Binary operators work on two operands ('binary‘ here means 2 operands, not in the sense of base-2 arithmetic).
  • 14. Balochistan University of IT & MS 14 Binary Operators + addition – subtraction * multiplication / division % remainder (e.g., 2%3 is 2), also called 'modulo' << left-shift (e.g., i<<j is i shifted to the left by j bits) >> right-shift & bit wise AND | bit wise OR ^ bit wise exclusive-OR
  • 15. Balochistan University of IT & MS 15 Binary Operators && logical AND (returns 1 if both operands are non-zero; else 0) || logical OR (returns 1 if either operand is non- zero; else 0) < less than (e.g., i<j returns 1 if i is less than j) > greater than <= less than or equal >= greater than or equal == equals != does not equal ? conditional operator
  • 16. Balochistan University of IT & MS 16 Assignment Operators • The most common assignment operator in C is the equals operator, =. – It is used to change the value of a variable. • For instance, the expression: f = 3.4; • causes the floating-point value 3.4 to be assigned to the variable f.
  • 17. Balochistan University of IT & MS 17 Assignment Operators • Multiple assignments are permissible in C. For example, i = j = k = 4; • causes the integer value 4 to be assigned to i, j, and k, simultaneously.
  • 18. Balochistan University of IT & MS 18 Assignment Operators • Assignment operators are really just binary operators. = assignment += addition assignment -= subtraction assignment *= multiplication assignment /= division assignment %= remainder/modulus assignment &= bit wise AND assignment |= bit wise OR assignment ^= bit wise exclusive OR assignment <<= left shift assignment >>= right shift assignment
  • 19. Balochistan University of IT & MS 19 Assignment Operators • Examples of using assignment operators: velocity = distance / time; force = mass * acceleration; count = count + 1;
  • 20. Balochistan University of IT & MS 20 Arithmetic Operators • There are four main arithmetic operators in C: addition + subtraction – multiplication * division / • There is no built-in exponentiation operator in C . Instead, there is a library function (pow) which carries out this operation. e.g. x2 is represented as x * x
  • 21. Balochistan University of IT & MS 21 Division, / • The division operator is special. – There is a vast difference between an integer divide and a floating-point divide – In an integer divide, the result is truncated i.e. the fractional part is discarded – If either the divisor or the dividend is a floating-point number, a floating-point divide is performed. – e.g. 19/10 = 1 – 19.0/10.0 = 1.9
  • 22. Balochistan University of IT & MS 22 Integer Division, / • Integer division returns just the result with no remainder. • For example: 15/3 = 5 16/3 = 5 17/3 = 5 3/15 = 0 0/4 = 0 4/0 = undefined
  • 23. Balochistan University of IT & MS 23 Arithmetic Operators Operation C Operator Algebraic Expression C Expression Addition + f + 7 f + 7 Subtraction – a – b a - b Multiplication * bm b * m Division / x / y or x ÷ y x / y Modulus % r mod s r % s
  • 24. Balochistan University of IT & MS 24 Parentheses, ( ) • Parentheses are used as punctuators to clarify or change the order in which operations are performed. • For example: a = 12 * (x + y)
  • 25. Balochistan University of IT & MS 25 Note • An alternative to turning one statement into two is to split long statements into multiple lines. – When breaking up a line, the preferred split point is where the parenthetic nesting is lowest. result = ( ( ( x + 1 ) * ( x + 1 )) - ( ( y + 1 ) * ( y + 1 ) ) ); 1233333332223333333211123333333222333333321 • The best place to break the line is where the nesting level is lowest; in this case at the – operator in the middle: result =(((x1 + 1) * (x1 + 1)) -((y1 + 1) * (y1 + 1)));
  • 26. Balochistan University of IT & MS 26 Operator Precedence • The operators within C are grouped hierarchically according to their precedence (i.e., their order of evaluation). • Amongst the arithmetic operators, * and / have precedence over + and –. – In other words, when evaluating expressions, C performs multiplication and division operations prior to addition and subtraction operations. • The rules of precedence can always be bypassed by the use of parentheses, ( , ).
  • 27. Balochistan University of IT & MS 27 Operator Precedence • For example: a - b / c + d is equivalent to the unambiguous expression a - (b / c) + d • since division takes precedence over addition and subtraction.
  • 28. Balochistan University of IT & MS 28 Operator Precedence and Associatively • The precedence of an operator gives the order in which operators are applied in expressions: the highest precedence operator is applied first, followed by the next highest, and so on. • The associatively of an operator gives the order in which expressions involving operators of the same precedence are evaluated.
  • 29. Balochistan University of IT & MS 29 Operator Precedence and Associatively
  • 30. Balochistan University of IT & MS 30 Operator Precedence and Associatively • Note: the +, – and * operators appear twice in the above table. The unary forms (on the second line) have higher precedence that the binary forms. • Operators on the same line have the same precedence, and are evaluated in the order given by the associatively. • To specify a different order of evaluation you can use parentheses. In fact, it is often good practice to use parentheses to guard against making mistakes in difficult cases, or to make your meaning clear.
  • 31. Balochistan University of IT & MS 31 Exercise Evaluate the value of following expressions m = 3, n=4 X = 2.5, y= 10 1. Z= m + n + x + y 2. Z= m + x * n + y 3. Z= X / y + m / n 4. Z= X – y * m + y / n 5. Z= X / 0
  • 32. Balochistan University of IT & MS 32 Mathematical Operators For Increment / Decrement k k :=k+1 k 4 3 k 9 K:= k-1 k 8
  • 33. Balochistan University of IT & MS 33 Exercise command x y z x = 2 2 unknown unknown x = x + 1 3 unknown unknown y = 2 3 2 unknown z = x + y 3 2 5 y = x + z 3 8 5 z = z - 2 3 8 3 x = 3 + z 6 8 3 x = y mod z 2 8 3
  • 34. Balochistan University of IT & MS 34 Relational Operators Symbol Operator Example < Less Than a < b > Greater Than a > b = Equal to a = b <= Less or Equal a <= b >= Greater or Equal a >=b <> Not Equal a <> b
  • 35. Balochistan University of IT & MS 35 Relational Operators a = 2, b=5 Symbol Operator OutPut < a < b 1 > a > b 0 = a = b 0 <= a <= b 1 >= a >=b 0 <> a <> b 1
  • 36. Balochistan University of IT & MS 36 Logical Operators Effects on relational expression’s results also AND, OR combines two or more Relational expressions, Results will be returned either TRUE( 1 ) or FALSE ( 0 ) regards with specific situations. Symbol Operator Example AND AND A< b AND c > d OR OR a < b OR c > d NOT NOT NOT ( a < b )
  • 37. Balochistan University of IT & MS 37 Logical Operators a = 2, b = 5, c = 9, d = 7 Symbol Operator Example AND a< b AND c > d 1 OR a < b OR c > d 1 NOT NOT ( a < b ) 0
  • 38. Balochistan University of IT & MS 38 Exercise Before Execution Expression After execution a = 2 , b = -1 a = a + b a= a = 2 a = -a a= x = 4 , y := 3 z = x > y + 1 z = , x =, y = a = 1 z = a != a z = x = 11, y = 6 z = x > 9 && y != 3 z = x = 11, y = 6 z = x = 5 || y != 3 z = x = 11, y = 6 z = ! ( x > 14 ) z= x = 11, y = 6 z = !( x > 9 AND y !=23) z= a = 13 , b = 4 z = a % b z= a = 3, x = a z = x = a; z= , x= , a=