Control Flow: Fall 21
Goal
- To understand the difference between
reference model languages and value model.
- To appreciate the ramification of
operators with side-effects.
- To understand iterators.
What will be covered ?
- L-Values, R-Values,
- Reference vs Value model of variables
- Immutiable objects
- Assignment operators
- Side-effects
- The principle of orthogonality
Code:
-
Values and References
-
L-Value/R-Value
- L-value is an expression
that refers to storage area
- R-value is an expression that denotes value
- Immutable objects only have R-values
- Under the value model, the context of an expression determines
whether is it an L or R Value.
- Examples
- Arrays
in
Perl
- Arrays in Python
- Immutiable objects --
-
Expressions -- and -- assignment
- What is the pure form of an expression?
- "=" in C,C++,C# and Java
- is both an expression and has a side effect
- is right associative
- Alternate assignments and unary operator -- appear to have the same
semantics.
int x = 0;
x = x + 1;
x = 0;
x += 1;
x = 0;
x++;
- Java code
static int n = 0;
static int [] A = new int[2];
static int f() {
if (n == 0) {
n=1;
} else {
n = 0;
}
return n;
}
static public void main (String [] arg) {
A[f()] = A[f()] +1; // 0 1
A[f()] += 1; // 0 2
A[0] = A[1] = n = 0;
A[f()] += 1; // 0 1
A[f()] = A[f()] + 1; // 2 1 }
}
Java evaluates the lhs first.
- In Python, the comma on the rhs defines a tuple of multiple
r-values and the comma on the lhs produces a tuple of l-values
- a , b = 15 , 3*25
-
Order of Evaluation of Operators and Parameters and Undefined
behavior
- C,Pascal, ADA:: the order of evaluations of the operands of
most operators are not specified in the language.
- C:: The same for evaluations of parameters
- Why is this a problem?
- Examples::
What is the value of x and a after ::
-
int x;
x = ++a + a; /* expression 1 */
a = 2;
x = a + ++a; /* expression 2 */
- What is printed out after ::
- int fun (int a, int b) {
return a + b;
}
int main(void){
int a =2;
int x;
x = fun (a,a++);
printf("fun(a,a++): x %d, a %d\n\n",x,a);
a = 2;
x = fun (a++,a);
printf("fun(a++,a): x %d, a %d\n\n",x,a);
}
- GCC output::
fun(a,a++): x 5, a 3
fun(a++,a): x 4, a 3
-
More on Operators with Side Effects
- These expressions change memory as well as returning a value.
x = (y = z)
x++
--y
- In C it is implementation dependent when side effects are applied
- What is the evaluation of the following expression containing
side-effect operators:
(Assume all side effect happens as soon as possible.)
- int n = 3;
int x = ++n * n++ - --n
- Assume left to right evaluation: (~> 12)
++
n
* n++ - --n
~> (++n * n++) - --n
where n = 3
~> (4 * n++) - --n
where n =4 (why?)
-->(4*4) - --n
where n =5
-->(4*4) - 4
where n =4
- Assume right to left evaluation: (~> 6)
- ++ n * n++
- --n
~> (++n *
n++) - --n where n =
3
~> (++n *
n++) - 2
where n = 2
-->(++n*2)
- 2
where n =3 (why?)
-->(4*2) -
2
where n =4
- Unlike C, Java specifies exactly when the side effects happen.
int i = 0;
i = i++;
System.out.println(i);
- Java notes the value of i,
increments i then makes the "=" of the noted
value.
-----------------------------------------------------
C Language:
- "Implementation-defined behavior is
defined
by the ISO C Standard in section 3.4.1 as:
unspecified behavior where each implementation documents how the choice
is made
EXAMPLE An example of implementation-defined behavior is the propagation
of the high-order bit when a signed integer is shifted right.
Any code that relies on implementation defined behaviour is only
guaranteed to work under a specific platform and/or compiler. Portable
programs should try to avoid such behaviour." (from clc-wiki)
- "Unspecified behaviour is
defined in ISO C99 in section 3.4.4, as:
behavior where [the standard] provides two or more possibilities and
imposes no further requirements on which is chosen in any instance
EXAMPLE An example of unspecified behavior is the order in which the
arguments to a function are evaluated.
In general, the careful programmer will avoid writing any code that
depends upon the outcome of aspects of C that have unspecified behavior,
as the code can easily behave differently when ported from one platform
to another, or even when it is built with a different compiler and
toolchain on the same platform.
It is even possible that the behavior could change from revision of a
compiler to the next, although that is less likely to occur in
practice." (from clc-wiki)
- "Undefined behaviour (UB) is
defined by the ISO/ANSI C Standard as:
behavior, upon use of a nonportable or erroneous program construct, of
erroneous data, or of indeterminately valued objects, for which this
International Standard imposes no requirements
NOTE Possible undefined behavior ranges from ignoring the situation
completely with unpredictable results, to behaving during translation or
program execution in a documented manner characteristic of the
environment (with or without the issuance of a diagnostic message), to
terminating a translation or execution (with the issuance of a
diagnostic message).
The C99 version of the Standard removed "indeterminately valued objects"
from this definition - in C99 the use of indeterminately valued objects
is unspecified behaviour." from clc-wiki)
A
good reference for C coding standard is the Software Engineering
Institude CERT Center.
--------------
Conditional Statements vs Conditional
Expressions:
- What is the primary difference between the conditional operator and
the conditional statement?
- What is the primary difference between �=� operator in C/Java/C++ and
the �=:� in Pascal ?
Relational and Boolean Expressions
- Relation operator compares two operands and returns a Boolean.
- Most languages have the type Boolean.
- C does not have the type boolean.
- 0 is considered false anything else is true.
- What is the value computed below?
a > b > c /* a = 10 , b = 9, c = 5 */
Boolean Expressions
- Design issue: How many parentheses are necessary.
- Short circuit evaluation -- lazy evaluation
- Order of Evaluation
- C specifies that short circuit operators are evaluated from left
to right.
- Ada and Pascal do not guarantee the order of evaluation.
Iterators
- Iterators are a separate thread of control
- Used to iterator over collections/containers
- True Iterators are found in Python, Ruby, c#
- C++, Java, Euclid do this using iterator objects.
- Rudy iterators use functional semantics.