CSADPRG_mod5_expressions_assignment_statements
this is unedited
arithmetic expressions
- one of the primary goals of the first high-level languages was the automatic evaluation of arithmetic expressions
- consist of operators, operands, parentheses, and function calls
- operators are unary (single operand), binary (two operands), or ternary (three operands)
design issues
- what are the operator precedence rules?
- what are the operator associativity rules?
- what is the order of operand evaluation?
- are there restrictions on operand evaluation side effects?
- does the language allow user-defined operator overloading?
- what type mixing is allowed in expressions
operator precedence vs operator associativity
- precedence: operator precedence rules for expression evaluation partially define the order in which adjacent operators of different precedence levels are evaluated
- associativity: operator associativity rules for expression evaluation define the order in which adjacent operators which the SAME precedence are evaluated
operator precedence: typical precedence levels
operator associativity: left and right associativity
- can be overriden with parentheses
conditional expressions
if (count == 0)
average = 0
else
average = sum / count
// when using a ternary operator
average = (count == 0)? 0: sum / count
operand evaluation order
- variables: evaluated by fetching the values from memory
- constants: sometimes fetch from memory; sometimes they're part of the machine language instruction
- parenthesized expressions: evaluate all operands and operators first
functional side effects
- occur when a function changes either one of its parameters or a global variable
a = 10;
b = a + fun(&a);
int a = 5;
int fun1() {
a = 17;
return 3;
}
void main() {
a = a + fun1();
}
solution to side effects
- disallow side effects
- no two-way parameters in functions
- no nonlocal references in functions
- disadvantages
- in c, to return more than one value you would need to place th values in a struct and return the struct
- global variables are necessary when efficiency is important and there is a need to avoid parameter passing
- write the language definition to state the order of evaluating operands and demand that implementors guarantee that order
- disadvantage
- limits compiler optimizations that involve reordering operand evaluations
- java's language definition guarantees that operands are evaluated left to right
- disadvantage
overloading
- multiple use of an operator, generally acceptable unless it inhibits readability or reliability
- dangers
- using the same symbol is detrimental to readability and mistakes can go undetected by the compiler
- some languages like c++ and c# allow the programmer to overload symbols. but what if a user re-defines + to mean multiplication?
narrowing and widening conversion
- narrowing: converts a value to a type that cannot store even approximations of all the values of the original type
- widening: converts a value to a type that can include at least approximations of all the values of the original type
- this is generally safer as it adds more precision
mixed-mode expressions
- expressions that can have operands of different types
- COERCION is an implicit type conversion initiated by the compiler or the runtime system, similar to typecasting
explicit type conversions
- in c-based languages, explicit conversions are called casts
float x = 2.222
int y = (int) x
errors in expressions
- if the language requires type checking then type errors cannot occur unless they are caused by coercions of operands
- other errors are due to the limitations of computer arithmetic (OVERFLOW or UNDERFLOW)
- or caused by the inherent limitations of arithmetic
- causes run-time errors or exceptions
int x = 2/0
in java
relational expressions
- relational operator: compares values of its two operands
- relational expression: has two operands and one relational operator
- the value of a relational expression is usually boolean, except when boolean is not a type in the language
boolean expressions
- consist of boolean varaibles, boolean constants, relational expressions, and boolean operators
- operators include: AND OR NOT OR XOR
- versions of C prior to C99 had no boolean type and no boolean values, so numeric values were used instead (0 meant false and all non-zero were considered true)
short circuit evaluation
- the SCE of an expression happens when the result is determined without evaluating all of the operands/operators
- exposes the potential problem of side-effects in expressions
assignment statements
- one of the central constructs in imperative languages
- allows the user to dynamically change the bindings of values to variables
- most use = sign and others may use :=
multiple assignments
- some languages provide multi-target, multi-source assignment statements
a, b = 100, 200
conditional targets
- some languages allow conditional targets on assignment statements
($flag ? $count1 : $count2) = 0
compound assignment operators
- a compound assignment operator is a shorthand method of specifying a commonly needed form of assignment
- syntax is the concatenation of the binary operator to the = operator
sum += value
unary assignment operator
- some languages include two special unary arithmetic operators ++ and --
- these combine increment and decrement operations with assignment
- can be postfix or prefix operators
assignment as an expression
- in some languages, the assignment statement produces a result so they can be used as operands in expressions
while ((ch = getchar()) ! =EOF) {}
sum = count = 0
- this may have poor readability
- this may have loss of error detection
if (x = y) vs if (x == y)
assignment in functional PL
- in pure functional languages, identifiers are just names so their values never change
- in ML, names are bound to values with the val declaration
val cost = quantity * price
- haskell:
let x = 3
mixed mode assignment
- assignment statements are frequently mixed mode
does the type of the expression have to be the same as the type of the variable being assigned, or can coercion be used in some cases of type mismatch?