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

PPTX
Relational algebra calculus
PDF
Dbms 12: Join
PDF
Dbms 14: Relational Calculus
PPTX
Relational algebra dbms (2130703) - 160920107003
PDF
Dbms 11: Relational Algebra
PPT
Lecture 06 relational algebra and calculus
PDF
Relational Algebra & Calculus
PPT
Relational algebra in dbms
Relational algebra calculus
Dbms 12: Join
Dbms 14: Relational Calculus
Relational algebra dbms (2130703) - 160920107003
Dbms 11: Relational Algebra
Lecture 06 relational algebra and calculus
Relational Algebra & Calculus
Relational algebra in dbms

What's hot (19)

PPTX
Chapter-7 Relational Calculus
PDF
Relational algebra in dbms
PPT
Ch6 formal relational query languages
Ā 
PPTX
Relational Algebra Introduction
PPT
Relational Algebra-Database Systems
PPT
Presentation on dbms(relational calculus)
PPT
Relational+algebra (1)
PDF
5 the relational algebra and calculus
Ā 
PDF
Digital communication lab lectures
PPT
E212d9a797dbms chapter3 b.sc2 (1)
PPT
E212d9a797dbms chapter3 b.sc2
PDF
Functions in discrete mathematics
PPTX
Relational algebra
PPT
Relational algebra.pptx
PPT
1643 y є r relational calculus-1
PPT
Linear functions and modeling
PPTX
Alg II 2-3 and 2-4 Linear Functions
PDF
A review of automatic differentiationand its efficient implementation
DOCX
Master of Computer Application (MCA) – Semester 4 MC0079
Chapter-7 Relational Calculus
Relational algebra in dbms
Ch6 formal relational query languages
Ā 
Relational Algebra Introduction
Relational Algebra-Database Systems
Presentation on dbms(relational calculus)
Relational+algebra (1)
5 the relational algebra and calculus
Ā 
Digital communication lab lectures
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2
Functions in discrete mathematics
Relational algebra
Relational algebra.pptx
1643 y є r relational calculus-1
Linear functions and modeling
Alg II 2-3 and 2-4 Linear Functions
A review of automatic differentiationand its efficient implementation
Master of Computer Application (MCA) – Semester 4 MC0079
Ad

Similar to Operators in C (20)

PPT
Arithmetic Operator in C
PDF
Unit ii chapter 1 operator and expressions in c
PPTX
Class_IX_Operators.pptx
PPTX
python ppt
PPT
Operation and expression in c++
PDF
introduction to c programming - Topic 3.pdf
PDF
Programming for Problem Solving
PPTX
Operators in C Programming
PPTX
OPERATORS IN C.pptx
Ā 
PPTX
OPERATORS IN C.pptx
Ā 
PPTX
Operators and expressions
PPTX
Class 2 variables, classes methods...
PPT
operator
PPTX
Unit-2_chap-4.pptx you from the heart of the
PDF
Coper in C
PDF
5_Operators.pdf
PPS
Order of Operations
PPTX
ESCM303 Introduction to Python Programming.pptx
PDF
SPL 6 | Operators in C
PDF
Module2.1_Programming_Branching_and_looping.pdf
Arithmetic Operator in C
Unit ii chapter 1 operator and expressions in c
Class_IX_Operators.pptx
python ppt
Operation and expression in c++
introduction to c programming - Topic 3.pdf
Programming for Problem Solving
Operators in C Programming
OPERATORS IN C.pptx
Ā 
OPERATORS IN C.pptx
Ā 
Operators and expressions
Class 2 variables, classes methods...
operator
Unit-2_chap-4.pptx you from the heart of the
Coper in C
5_Operators.pdf
Order of Operations
ESCM303 Introduction to Python Programming.pptx
SPL 6 | Operators in C
Module2.1_Programming_Branching_and_looping.pdf
Ad

More from yarkhosh (6)

PPT
Decisions in C or If condition
PPT
Algorithm
PPT
Math Functions in C Scanf Printf
PPT
Data Types in C
PPT
Escape Sequences and Variables
PPT
Introduct To C Language Programming
Decisions in C or If condition
Algorithm
Math Functions in C Scanf Printf
Data Types in C
Escape Sequences and Variables
Introduct To C Language Programming

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Introduction to Artificial Intelligence
PDF
Understanding Forklifts - TECH EHS Solution
PPT
Introduction Database Management System for Course Database
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Designing Intelligence for the Shop Floor.pdf
System and Network Administraation Chapter 3
How to Choose the Right IT Partner for Your Business in Malaysia
2025 Textile ERP Trends: SAP, Odoo & Oracle
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Introduction to Artificial Intelligence
Understanding Forklifts - TECH EHS Solution
Introduction Database Management System for Course Database
Navsoft: AI-Powered Business Solutions & Custom Software Development
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Digital Systems & Binary Numbers (comprehensive )
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Computer Software and OS of computer science of grade 11.pptx
Design an Analysis of Algorithms II-SECS-1021-03
history of c programming in notes for students .pptx
Reimagine Home Health with the Power of Agentic AI​
Designing Intelligence for the Shop Floor.pdf

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=