CHAPTER 6
EXPRESSIONS AND ASSIGNMENT
STATEMENTS
INTRO:
- Expressions are the
fundamental means of specifying computations in a programming
language. It is important for
programmers to understand the syntax and the semantics of expressions.
- To understand
expression evaluation, it is necessary to be familiar with the orders of
operator and operand evaluation.
The operator evaluation order of expressions is governed by the associativity
and precedence rules of the language.
- The essence of the
imperative programming languages is the dominant role of assignment
statements. The purpose of
assignment statement is to change the value of a variable. So an integral part of all imperative
languages is the concept of variables whose values change during program
execution.
ARITHMETIC EXPRESSIONS
- Most of the
characteristics of arithmetic expressions in programming languages were
inherited from conventions that had evolved in mathematics. In programming languages, arithmetic
expressions consist of operators, operands, parentheses, and function
calls.
- The operators can be unary, meaning they have single
operand, or binary, meaning
they have two operands.
- C, C++, and Java
include a ternary operator,
which has three operands.
- In most imperative
languages binary operators are infix which mend they appear between their
operands.
- The purpose of an
arithmetic expression is to specify an arithmetic computation. The two actions of this are:
- fetching the operands,
usually from memory,
- executing the arithmetic
operations on those operands
OPERATOR EVALUATION ORDER
PRECEDENCE
- The order of an
expression depends on the order of evaluation of the operators
- The operator precedence
rules for expression evaluation define the order in which the operators of
different precedence levels are evaluated. These rules are based on the hierarchy of operator
priorities.
- Unary addition is
called identity operator
because it usually has no associated operation and thus has no effect on
its operand.
ASSOCIATIVITY
- Ex. A – B + C – D
- When an expression
contains two adjacent occurrences of operators with the same level of
precedence, the question of which operator is evaluated first is answered
by the associativity rules of
the language.
- An operator can either
have left or right associativity, meaning that the leftmost occurrence is
evaluated first or the rightmost occurrence is evaluated first,
respectively.
- A
– B + C in FORTRAN the left
opertor is evaluated first.
- Exponentiation in
FORTRAN: A ** B ** C the right operator is evaluated
first.
- A ** B ** C, in Ada is
illegal, but, (A ** B) ** C is legal.
- The associativity rules
of some imperative languages are:
Language Associativity Rule
FORTRAN Left:
*, /, +, -
Right: **
Pascal Left: All
C Left: postfix ++, postfix --, *, /,
%, binary +, binary –
Right: prefix ++, prefix --, unary +, unary –
C++ Left: *,
/, %, binary +, binary –
Right:
++, --, unary -, unary +
Ada Left: all except **
Non-associative: **
PARENTHESES
- Programmers can alter
the precedence and associativity rules by placing parentheses in expressions
- (A + B) * C here the addition is done first.
CONDITIONAL
EXPRESSIONS
- Sometimes if-then-else statements are used
to perform a conditional expression assignment.
- The ternary operator,
?:, which is part of C, C++, and Java is used to form conditional
expressions.
If (count = 0)
Then average := 0
Else average := sum / count
Can
become:
Average = (count == 0) ? 0 : sum /
count;
OPERAND EVALUATION ORDER
SIDE
EFFECTS
- A side effect of a function, called a functional side effect, occurs when the function changes
either one of its parameters or a global variable. (A global variable is declared outside
the function but is accessible in the function.)
- Ex. A + FUN(A)
if FUN does not have the side effect of changing A,
then the order of evaluation of the two operands, A and FUN(A), has no effect
on the value of expression. But, if FUN
changes A, there is an effect.
OVERLOADED OPERATORS
- Operator overloading is the multiple use of
operators.
- Ex., the “+” is used
for addition and for string concatenation in Java.
- The “&’ in C is a
problem and overloading the minus operator is also a problem.
TYPE CONVERSIONS
- Type conversions are
either narrowing or widening.
- A narrowing conversion converts a value to a type that cannot
store even approximations of all the values of the original type.
- Ex., converting a double to float (double is
larger than float)
- A widening conversion converts a value to a type that can
include at least approximations of all of the values of the original
type. Ex., converting an int to a float
COERCION
IN EXPRESSIONS
- One of the design
decisions concerning arithmetic expressions is whether an operator can
have operands of different types.
Languages that do allow such expressions, which are called mixed-mode expressions, must
define conventions for implicit operand type conversions, called coercions, because computers
usually do not have binary operations that take operands of different
types.
- Coercions defined as an implicit type conversion
that is initiated by the compiler.
RELATIONAL AND BOOLEAN
EXPRESSIONS
RELATIONAL
EXPRESSIONS
- A relational operator is an operator that compares the value if
its two operands.
- A relational expression has two operands and one relational operator. The value of a relational expression is
Boolean, except when Boolean is not a type in the language. The relational operators are usually overloaded
for a variety of types
- The syntax of the
relational operators in some languages:
Operation Pascal Ada C FORTRAN77
Equal = = == .EQ.
Not
equal <> /= != .NE.
Greater
than > > > .GT.
Less
than < < < .LT.
Greater
than or equal >= >= >= .GE.
Less
than or equal <= <= <= .LE.
BOOLEAN
EXPRESSIONS
- Boolean expressions
consist of Boolean variables, Boolean constants, relational expressions,
and Boolean operators.
- The operators include
those for the AND, OR, and NOT operations, and sometimes for exclusive OR
and equivalence.
- The Boolean operators
are evaluated like the arithmetic operators, in a hierarchical precedence
order.
SHORT-CIRCUIT EVALUATION
- A short-circuit evaluation of an expression is one in which the
result is determined without evaluating all of the operands and/or
operators.
- Ex., (13 * A) * (B / 13 – 1) is independent of the value (B / 13 – 1) if a is 0. Because
0*x = 0 for any x. So if A is 0
there is no need to evaluate (B / 13 – 1).
- Short-circuit
evaluation of expressions exposes the problem of allowing side effects in
expressions.
ASSIGNMENT STATEMENTS
- The assignment
statement is one of the most central constructs in imperative
languages. It provides the
mechanism by which the user can dynamically change the bindings of values
to variables.
SIMPLE
ASSIGNMENTS
- The general syntax of
simple assignments is;
<target_variable> <assignment_operator> <expression>
- FORTRAN, BASIC, PL/1,
C, C++, and Java use the equal sign for the assignment operator. This can lead to confusion if the equal
sign is also used as a relational operator, as it is in PL/1 and BASIC.
COMPOUND
ASSIGNMENT OPERATORS
- A compound assignment operator is a shorthand method of
specifying a commonly needed form of assignment. The form of assignment that can be abbreviated with this technique
has the destination variable also appearing as the first operand in the
expression on the right side as in,
a = a + b
- Compound assignment
operators were introduced by ALGOL 68
- The syntax of C's
compound assignment operators is the desired binary operator to the =
operator.
For example, sum += value;
Is equivalent to sum = sum + value;
UNARY
ASSIGNMENT OERATORS
- C, C++, and Java
include 2 special unary arithmetic operators that are actually abbreviated
assignments. They combine
increment and decrement operators with assignment.
- ++ used for increment
- -- used for decrement
ASSIGNMENT
AS AN EXPRESSION
- C, C++, and Java the
assignment statement produces a result, which is the same as the value
assigned to the target. So, it can
be used as an expression and as an operand in other expressions.
- The disadvantage of
allowing assignment statements to be operands in expressions is that it
provides yet another kind of expression side effect. This type of side effect can lead to
expressions that are difficult to read and understand.