Chapter 6
Chapter 6
Figure 6.1 Operator precedence levels in Fortran, Pascal, C, and Ada. The operator s at the top of the figure group most tightly.
• Short-circuiting
– Consider (a < b) && (b < c):
• If a >= b there is no point evaluating whether b < c
because (a < b) && (b < c) is automatically false
– Other similar situations
if (b != 0 && a/b == c) ...
if (*p && p->foo) ...
if (unlikely_condition &&
very_expensive function()) ...
• Assignment
– statement (or expression) executed for its side
effect - key to most programming languages
you have seen so far.
– assignment operators (+=, -=, etc)
• Handy shortcuts
• avoid redundant work (or need for optimization)
• perform side effects exactly once
– Example: A[index_fn(i) ]++;
– versus A[index_fn(i) ] = A[index_fn(i) ] + 1;
Copyright © 2009 Elsevier
Multiway Assignment
• Side Effects
– often discussed in the context of functions
– a side effect is some permanent state change
caused by execution of function
• some noticable effect of call other than return value
• in a more general sense, assignment statements
provide the ultimate example of side effects
– they change the value of a variable
• Sequencing
– specifies a linear ordering on statements
• one statement follows another
– very imperative, Von-Neuman
• In assembly, the only way to “jump” around is
to use branch statements.
• Early programming languages mimicked this,
such as Fortran (and even Basic and C).
Copyright © 2009 Elsevier
The end of goto
• Selection
– Fortran computed gotos
– jump code
• for selection and logically-controlled loops
• no point in computing a Boolean value into a register, then
testing it
• instead of passing register containing Boolean out of
expression as a synthesized attribute, pass inherited
attributes INTO expression indicating where to jump to if
true, and where to jump to if false
Copyright © 2009 Elsevier
Selection
• Recursion
– equally powerful to iteration
– mechanical transformations back and forth
– often more intuitive (sometimes less)
– naïve implementation less efficient
• no special syntax required
• fundamental to functional languages like Scheme
• Tail recursion
– No computation follows recursive call
int gcd (int a, int b) {
/* assume a, b > 0 */
if (a == b) return a;
else if (a > b) return gcd (a - b,b);
else return gcd (a, b – a);
}