Presentation is loading. Please wait.

Presentation is loading. Please wait.

Http://flic.kr/p/aGhubM Control Flow.

Similar presentations


Presentation on theme: "Http://flic.kr/p/aGhubM Control Flow."— Presentation transcript:

1 Control Flow

2 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

3 Sequencing Design Issue: Operator Syntax
Cambridge Polish Notation (Used in Lisp, ML, and R)

4 Sequencing Design Issue: Variables and Assignment
l-values: Locations r-values: Values

5 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

6 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

7 C++ Value/Pointer Demo

8 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

9 Orthogonality-Related Pitfall
C++ allows assignments within expressions, which leads to this problem This bug can be very difficult to spot!

10 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

11 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

12 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

13 Unstructured Control Flow with goto
Jump to label “10” Activity: Write a loop using gotos How does goto compare to while loops?

14 What makes this confusing?
Call stack must be repaired (typically by “unwinding”) 2 1 3

15 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)

16 Design Issue: Iteration versus Recursion
Let’s make sure you remember how to think recursively

17 Code a recursive implementation of this:
Solution:

18 How could you implement this using iteration (i.e., loops)?
Simulate the call stack

19 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

20 Tail Recursion Additional computation does not follow recursive call
Easy for compiler to optimize:

21 What’s next? Homework 3 due in one week

22 LEFTOVERS

23

24

25

26


Download ppt "Http://flic.kr/p/aGhubM Control Flow."

Similar presentations


Ads by Google