EXPRESSIONS AND ASSIGNMENT
- 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
- 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
- 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
- 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
- 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
- Unary addition is
called identity operator
because it usually has no associated operation and thus has no effect on
- 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
- An operator can either
have left or right associativity, meaning that the leftmost occurrence is
evaluated first or the rightmost occurrence is evaluated first,
– B + C in FORTRAN the left
opertor is evaluated first.
- Exponentiation in
FORTRAN: A ** B ** C the right operator is evaluated
- A ** B ** C, in Ada is
illegal, but, (A ** B) ** C is legal.
- The associativity rules
of some imperative languages are:
Language Associativity Rule
*, /, +, -
Pascal Left: All
C Left: postfix ++, postfix --, *, /,
%, binary +, binary –
Right: prefix ++, prefix --, unary +, unary –
C++ Left: *,
/, %, binary +, binary –
++, --, unary -, unary +
Ada Left: all except **
- Programmers can alter
the precedence and associativity rules by placing parentheses in expressions
- (A + B) * C here the addition is done first.
- 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
If (count = 0)
Then average := 0
Else average := sum / count
Average = (count == 0) ? 0 : sum /
OPERAND EVALUATION ORDER
- 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.
- Operator overloading is the multiple use of
- 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 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
- 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
- Coercions defined as an implicit type conversion
that is initiated by the compiler.
RELATIONAL AND BOOLEAN
- 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.
equal <> /= != .NE.
than > > > .GT.
than < < < .LT.
than or equal >= >= >= .GE.
than or equal <= <= <= .LE.
- 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
- The Boolean operators
are evaluated like the arithmetic operators, in a hierarchical precedence
- A short-circuit evaluation of an expression is one in which the
result is determined without evaluating all of the operands and/or
- 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).
evaluation of expressions exposes the problem of allowing side effects in
- 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
- 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.
- 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 =
For example, sum += value;
Is equivalent to sum = sum + value;
- 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
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.