CSADPRG_mod4_assignment_statements
original file
this is unedited
assignment statements
- one of the central constructs in imperative languages
- allows user to dynamically change the bindings of values to variables
simple assignments
- nearly all use = but all of them must use a different symbol for equalty relational operator to avoid confusion
=
for FORTRAN, BASIC, PL/I, C, C++, Java:=
for ALGOLs, Pascal, Modula-2, Ada
In PL/1: A = B = C;
- first is the assignment operator
- second is the comparison operator
- The value '1'B is assigned to A if B is equal to C; otherwise, the value '0'B is assigned. '1'B stands for true and '0'B for false.
int main()
{
int a = 3;
int b = 2;
int c = 1;
a = b = c;
printf("%d %d %d",a,b,c);
}
// output 1 1 1
- since the value of c which is 1 is assigned to b, and the value of b which is now 1 is assigned to a. Note that the assignment operator is right associative.
multiple assignments
- perl, ruby, lua, and python allow multiple-target, multiple-source assignment statements like
a, b = 100, 200
so swapping can be done by doing
x =2
y =3
x,y = y,x
print(x,y)
conditional targets
- perl, c, c++, and java allow conditional targets on assignment statements
($flag ? $count1 : $count2) = 0;
this is equivalent to
if ($flag) {
$count1 = 0;
} else {
$count2 = 0;
}
compound assignment operators
- shorthand method of specifying a commonly needed form of assignment
- part of c-based languages, perl, javascript, python, and ruby
- syntax: concat the binary operator to the = operator like
sum += value
unary assignment operators
- c-based languages, perl, and javascript have 2 pecial unary arithmetic operators
++
and--
- combine increment and decrement operations with assignment
- appear as postfix or prefix
sum = ++ count;
// is equivalent to
count = count + 1;
sum = count;
//POSTFIX
sum = count ++;
//is equivalent to
sum = count;
count = count + 1;
assignment as expression
- in c, c++, java, perl, and javascript the assignment statement produces a result so they can be used as operands in expressions
c: while ((ch = getchar()) != EOF) { ... }
- Note that the assignment must be parenthesized because the assignment operator has a lower precedence than the relational operators.
c also allows multiple-target assignments likesum = count = 0
- the disadvantage is that it can also lead to side effects. this side effect leads to poor readability
a = b + (c = d / b) - 1
- cannot be read as an expression like in math
- we need to list the instructions
Assign d / b to c
Assign b + c to temp
Assign temp - 1 to a
if (x=y) instead of if (x==y)
- loss of error detection that leads to errors
assignment in functional programming languages
- in pure functional languages, identifiers are just names so their values never change.
- ML: names are bound to values with the val declaration like
val cost = quantity * price
- if
cost
appears on the left side of a subsequentval
declaration, that declaration creates a new version of the namecost
which has no relationship with the previous version (which is then hidden)
- if
in haskell, if an identifier x is declared by let x = 3
then the value of x will always be 3. it can't be changed by assignment.
- haskell declarations as introducing constants not variables
mixed mode assignment
- assignment statements are frequently mixed-mode (refer to coercion in previous modules)
- design question: 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?
- in c, c++, and perl: many possible type mixes are legal so coercion is freely applied. has the same rules as mixed-mode expressions
- java and c# allow mixed-mode assignment if the coercion is widening. (int -> float but not vice versa)
- functional languages have no mixed-mode assignment