Session 5 : Expression and Assignment Statements
October21
Introduction
- Expressions are the fundamental means of specifying computations in a programming language
- To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation
- Essence of imperative languages is dominant role of assignment statements
Arithmetic Expressions
- Arithmetic evaluation was one of the motivations for the development of the first programming languages
- Arithmetic expressions consist of operators, operands, parentheses, and function calls
Overloaded Operators
- Use of an operator for more than one purpose is called operator overloading
- Some are common (e.g., + for int and float)
- Some are potential trouble (e.g., * in C and C++)
- Loss of compiler error detection (omission of an operand should be a detectable error)
- Some loss of readability
- C++, C#, and F# allow user-defined overloaded operators
- When sensibly used, such operators can be an aid to readability (avoid method calls, expressions appear natural)
- Potential problems:
- Users can define nonsense operations
- Readability may suffer, even when the operators make sense
Type Conversions
- A narrowing conversion is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int
- A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type e.g., int to float
Relational and Boolean Expressions
- Relational Expressions
- Use relational operators and operands of various types
- Evaluate to some Boolean representation
- Operator symbols used vary somewhat among languages (!=, /=, ~=, .NE., <>, #)
- JavaScript and PHP have two additional relational operator, === and !==
- Similar to their cousins, == and !=, except that they do not coerce their operands
- Ruby uses == for equality relation operator that uses coercions and eql? for those that do not
- Boolean Expressions
- Operands are Boolean and the result is Boolean
- Example operators
- C89 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)
Short Circuit Evaluation
- An expression in which the result is determined without evaluating all of the operands and/or operators
- Example: (13 * a) * (b / 13 – 1)
If a is zero, there is no need to evaluate (b /13 – 1)
- Problem with non-short-circuit evaluation
index = 0;
while (index <= length) && (LIST[index] != value)
index++;
When index=length, LIST[index] will cause an indexing problem (assuming LIST is length – 1 long)
- C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||), but also provide bitwise Boolean operators that are not short circuit (& and |)
- All logic operators in Ruby, Perl, ML, F#, and Python are short-circuit evaluated
- Ada: programmer can specify either (short-circuit is specified with and then and or else)
- Short-circuit evaluation exposes the potential problem of side effects in expressions
e.g. (a > b) || (b++ / 3)
Assignment Statements
- The general syntax
<target_var> <assign_operator> <expression>
- The assignment operator
= Fortran, BASIC, the C-based languages
:= Ada
- = can be bad when it is overloaded for the relational operator for equality (that’s why the C-based languages use == as the relational operator)
Mixed-Mode Assignment
- Assignment statements can also be mixed-mode
- In Fortran, C, Perl, and C++, any numeric type value can be assigned to any numeric type variable
- In Java and C#, only widening assignment coercions are done
- In Ada, there is no assignment coercion