0% found this document useful (0 votes)
8 views30 pages

Chapter 07 Expressions and Assignments

Chapter 7 discusses expressions and assignment statements, which are essential for specifying computations in programming. It covers various topics including arithmetic expressions, operator precedence, conditional expressions, and the implications of functional side effects. The chapter also addresses type conversions, operator overloading, and the syntax of assignment statements across different programming languages.

Uploaded by

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

Chapter 07 Expressions and Assignments

Chapter 7 discusses expressions and assignment statements, which are essential for specifying computations in programming. It covers various topics including arithmetic expressions, operator precedence, conditional expressions, and the implications of functional side effects. The chapter also addresses type conversions, operator overloading, and the syntax of assignment statements across different programming languages.

Uploaded by

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

CHAPTER 7

EXPRESSIONS AND
ASSIGNMENT
STATEMENTS

Spring 2022
Where are
we ?
We have studied Names and Types
2

 How they are declared, and their semantic


meanings
 Array X means a sequence of same elements …

 Next, let us study program structure


 Sequential code
 Branches, loops, function calls, …

 And their semantic meanings

CSC 461 May 1, 2025


Introducti
3
on
 Expressions and assignments are
fundamental in specifying computations
 Produce sequential code

 Let us study various issues associated


with
 Expressions
 Assignments

CSC 461 May 1, 2025


Arithmetic
4
Expressions
 Example
( 3 + b ) * a – func(14)

operators, operands, parentheses, and


function calls

CSC 461 May 1, 2025


Operato
rs
5  Different operators
 A unary operator has one operand
– –a, a ++, …
 A binary operator has two operands
a + b, a – b, …
 A ternary operator has three operands
(a>b) ? a : b // result is a if (a>b), or b if not
 Exercise:
 What is the value? a=10; a++; +
+a;
 legal or not ? ++a, a++, ++ a ++

CSC 461 May 1, 2025


Modulo
Operation
6  a modulo b
 Returns the remainder of division of a by b
 a = b  q + r, return r
 Straightforward if both a and b are positive
values
 What if a or b is negative ?
 Language-dependent, implementation-
dependent
 Ada:
mod: result has the same sign as divisor
(-10) mod 3 = 1
rem: result has the same sign as dividend
(-10) rem 3 = -1
CSC 461 May 1, 2025
Order of
7
Computation
 What is the order of computation if
a op1 b op2 c op3 d

 Operator precedence rule


 Define the order in which “adjacent” operators
of different precedence levels are evaluated
 Typical precedence levels
 parentheses
 unary operators
 ** (if the language supports it)
 *, /
 +, -

CSC 461 May 1, 2025


Order of Computation
8
(cont.)
 Operator associativity rule
 define the order in which adjacent operators with the
same precedence level are evaluated
 Typical associativity rules
 Left to right
a + b + c, a * b * d
 Exponentiation **
 Fortran: from right to left 2 ** 3 ** 4 = 2 81
≠8
4

 Ada: must use a parenthesis (2 ** 3) ** 4


 Visual basic 2 ** 3 ** 4 = 8 4 ≠ 2 81

CSC 461 May 1, 2025


Operator Associativity
9
Rule
 Only for operators at the same level
 Ada: (-10) mod 3 = 1
- 10 mod 3 = - (10 mod 3) = -1

 What if you are not sure of the


precedence or associativity ?
 What is of higher priority?
a * c mod 3
 Use parentheses !!!

a* ( c mod 3 )
CSC 461 May 1, 2025
Conditional
10
Expressions
 Conditional Expressions
 C-based languages (e.g., C, C++)
 An example:
average = (count == 0)? 0 : sum / count

 Evaluates as if written like


if (count == 0) average = 0
else average = sum /count

CSC 461 May 1, 2025


Operan
ds
11

 Operand evaluation order


1. Variables: fetch the value from memory
2. Constants: sometimes a fetch from memory;
sometimes the constant is in the machine
language instruction
3. Parenthesized expressions: evaluate all operands
and operators first
 Evaluation at the assembly level
 Idea: load to a register ld R1,
 Variable: memory location 100, 0(100)
 Constant: integer 100, li R1, 100
 Expression: after computation, add R1,
R2, R3
CSC 461 May 1, 2025
Functional Side
Effects
12

 Def: a function has side effects when it changes a two-way


parameter or a non-local variable
 Example: Int a = 5;
 The results depends on the
evaluation order Int fun1() {
 From left to right a = 17;
evaluate a, then func() func (int & x) Return 3;
=? { } /* end of fun1 */
 From right to left x = 12;
evaluate func(), then a return x+1; Void main() {
=?
} a = a + fun1();
} /* end of main */

The value computed for a in main depends on the order of


evaluation of the operands in the expression a + fun1(). The
value of a will be either 8(if a is evaluated first) or 20(if the
function call is evaluated first).
CSC 461 May 1, 2025
Solutions to Function Side
13
Effects
 Two possible solutions to the problem
when design a language
1. Disallow functional side effects
 No two-way parameters in functions
 No non-local references in functions
 Advantage: it works!
 Disadvantage: inflexibility of two-way
parameters and non-local references

2. Fix the operand evaluation order


 Disadvantage: limits some compiler
optimizations

CSC 461 May 1, 2025


Overloaded
Operators
14

 Use of an operator for more than one


purpose is called operator overloading
 Some are common (e.g., + for int and
float)

 Some have potential trouble: “/”


 In C/C++: “/” is overloaded for both int
and float
int a = 10, b = 4;
float c = a / b; what is the value of c ?
In Pascal: “div” for int, “/” for float
c:= a/b

CSC 461 May 1, 2025


Operator Overloading and
Readability
15

 As discussed, readability is one of the


most important evaluation criteria

 Operator overloading can aid readability


Matrix A, B, C, D;
A*B + C*D
MatrixAdd(MatrixMult(A,B), MatrixMult(C,D))

CSC 461 May 1, 2025


Type
16
Conversions
 Why “float c =10/3;” , c has value 3 ?

 narrowing conversion:
 Converts an object to a type that cannot include all of the
values of the original type
e.g. float to int
 widening conversion
 Converts an object to a type that can include at least
approximations to all of the values of the original type
e.g. int to float

CSC 461 May 1, 2025


Type Conversions: Mixed
17
Mode
 A mixed-mode expression is one that
has operands of different types
 A coercion is an implicit type conversion
 Disadvantage:
They decrease in the type error detection ability of the
compiler
 In most languages, all numeric types are
coerced in expressions, using widening
conversions

CSC 461 May 1, 2025


Explicit Type
18
Conversions
 Explicit Type Conversions
 Called casting in C-based language
 Examples
 C: (int) angle
 Ada: Float (sum)

Note that Ada’s syntax is similar to function


calls

CSC 461 May 1, 2025


Relational and Boolean
Expressions
19

 Relational Expressions
 Use relational operators and operands of
various types
 Evaluate to some Boolean representation
 Operator symbols used vary somewhat
among languages (!=, /=, .NE., <>, #)

CSC 461 May 1, 2025


Relational and Boolean
Expressions
20

 Boolean Expressions
 Operands are Boolean and the result is
Boolean
 Example operators

FORTRAN 77 FORTRAN 90 C Ada


.AND. and && and
.OR. or || or
.NOT. not ! not
xor
CSC 461 May 1, 2025
No Boolean Type
21
in C
 C has no Boolean type--it uses int type
with 0 for false and nonzero for true
 One odd characteristic of C’s
expressions:
a < b < c is a legal expression, but the
result is not what you might expect:
 Left operator is evaluated, producing 0 or 1
 The evaluation result is then compared with
the third operand (i.e., c)

CSC 461 May 1, 2025


Operator Precedence (with
22
logic op)
 Precedence of C-based operators
prefix ++, --
unary +, -, prefix ++, --, !
*,/,%
binary +, -
<, >, <=, >=
=, !=
&&
||

CSC 461 May 1, 2025


Short Circuit
23
Evaluation
 An expression in which the result is determined
without evaluating all of the operands and/or
operators

 Reason:
a && b if a is false, then result is
false
a || b if a is true, then result is true

 Example
( a>2) || ( b/13+c*23+d >1024)

CSC 461 May 1, 2025


Sometimes Short-circuit is
24
a Must
 Sometimes, we can have problem
without short-circuit evaluation
 index = 1;
while (index <= length) && (LIST[index] != value)
index++;
when index=length, LIST [index] will cause an
indexing problem (assuming LIST has length -1
elements)

CSC 461 May 1, 2025


Assignment
25
Statements
 The general syntax
<target_var> <assign_operator> <expression>
 The assignment operator
= FORTRAN, BASIC, PL/I, C, C++, Java
:= ALGOLs, Pascal, Ada
 = can be bad when it is overloaded for
the relational operator for equality

CSC 461 May 1, 2025


Conditional
26
Targets
 Conditional targets (C, C++, and Java)
(flag)? total : subtotal = 0

Which is equivalent to

if (flag)
total = 0
else
subtotal = 0

CSC 461 May 1, 2025


Compound
27
Operators
 A shorthand method of specifying a
commonly needed form of assignment
 Introduced in ALGOL; adopted by C
 Example

a = a + b

is written as

a += b
CSC 461 May 1, 2025
Assignment
Statements:
28
Unary Assignment
Operators
Unary assignment operators in C-based
languages combine increment and
decrement operations with assignment
 Examples
sum = ++count (count incremented, added to
sum)
sum = count++ (count incremented, added to
sum)
count++ (count incremented)
-count++ (count incremented then negated)
CSC 461 May 1, 2025
Assignment as an
29
Expression
 In C, C++, and Java, the assignment
statement produces a result and can be
used as operands
 An example:
while ((ch = getchar())!= EOF){…}

ch = getchar() is carried out; the result


(assigned to ch) is used as a conditional
value for the while statement
CSC 461 May 1, 2025
Mixed-Mode
30
Assignment
 Assignment statements can also be mixed-
mode, for example
int a, b;
float c;
c = a / b;
 In Pascal, integer variables can be assigned
to real variables, but real variables cannot
be assigned to integers
 In Java, only widening assignment coercions
are done
 In Ada, there is no assignment coercion
CSC 461 May 1, 2025

You might also like