http://flic.kr/p/aGhubM Control Flow
Categories of Control Mechanisms Sequencing (e.g., expression evaluation) Selection (e.g., if/else) Iteration (e.g., loops) Procedural abstractions (e.g., subroutines) Recursion (e.g., recursive function calls) Concurrency (e.g., threads) Exception handling and speculation (e.g., try/catch) Nondeterminacy
Sequencing Design Issue: Operator Syntax Cambridge Polish Notation (Used in Lisp, ML, and R)
Sequencing Design Issue: Variables and Assignment l-values: Locations r-values: Values
Two Models of Variables Value Model Reference Model a = 4; b = 2; c = 2; For what types of variables does Java use each model? Primitive types use value, and Object types use reference
Java References versus C/C++ Pointers MyObject objA = new MyObject(); MyObject objB = null; objB = objA; // A and B refer to same val objB.myMethod(); C++: MyObject* objA = new MyObject; MyObject* objB = null; objB = objA; // A and B refer to same val (*objB).myMethod(); // Long form objB->myMethod(); // Sugary form In C++, pointers can reference any type and must be explicitly dereferenced to get to the referent value
C++ Value/Pointer Demo
Design Issue: Orthogonality Extent to which lang. features can be used in any combination Algol 68 Example: a gets d or e a gets last value This block returns 5 Algol has no separate notion of statement and expression
Orthogonality-Related Pitfall C++ allows assignments within expressions, which leads to this problem This bug can be very difficult to spot!
Design Option: Multiway Assignments Supported in Clu, ML, Perl, Python, and Ruby is like What do you suppose this does? Swap! (r-values must be gotten before assignment) What do you suppose is happening here? Function returns tuple
Design Issue: Undefined Order of Evaluation Which of the underlined expression gets evaluated first? Does it matter? In some languages, order is undefined to allow for compiler optimizations (In Java and C# it’s l-to-r, though) Order matters if there are operations with side effects
Design Issue: Short-Circuit Evaluation What happens in these examples if all parts of expression are evaluated? Expensive function always evaluated even when pointless Possible segmentation fault! (null pointer dereference) Short-circuit evaluation helps by only evaluating subexpressions as needed
Unstructured Control Flow with goto Jump to label “10” Activity: Write a loop using gotos How does goto compare to while loops?
What makes this confusing? Call stack must be repaired (typically by “unwinding”) 2 1 3
Famous Article: “Go To Statement Considered Harmful” by Edsger W Famous Article: “Go To Statement Considered Harmful” by Edsger W. Dijkstra (1968) Gist of argument: Goto makes it hard to reason about potential program behavior This article and others lead to structured programming (if/else, while, etc. statements)
Design Issue: Iteration versus Recursion Let’s make sure you remember how to think recursively
Code a recursive implementation of this: Solution:
How could you implement this using iteration (i.e., loops)? Simulate the call stack
In general do recursive or iterative solutions perform better? Iterative because each recursive call creates another stack frame However, there are special cases, where recursion does well
Tail Recursion Additional computation does not follow recursive call Easy for compiler to optimize:
What’s next? Homework 3 due in one week
LEFTOVERS