Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adapted from Scott, 20061 Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.

Similar presentations

Presentation on theme: "Adapted from Scott, 20061 Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott."— Presentation transcript:

1 Adapted from Scott, 20061 Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott

2 Adapted from Scott, 20062 Control Flow Basic paradigms for control flow: –Sequencing –Selection –Iteration –Subroutines, recursion (and related control abstractions, e.g. iterators) –Nondeterminacy – ordering unspecified –Concurrency – parallel or interleaved execution

3 Adapted from Scott, 20063 Expression Evaluation Infix, prefix operators Precedence, associativity (see Figure 6.1) –C has 15 levels - too many to remember –Pascal has 3 levels - too few for good semantics if A < B and C < D then…  A < (B and C) < D –Fortran has 8 –Ada has 6 –Lesson: The programmer working in several languages will use parentheses! Usually: mult/div>add/sub>relational>logical

4 Adapted from Scott, 20064 Expression Evaluation

5 Adapted from Scott, 20065 Expression Evaluation 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( f || messy() )...

6 Adapted from Scott, 20066 Expression Evaluation Variables as values vs. variables as references –value-oriented languages A variable is a named container for a value (r-value) C, Pascal, Ada –reference-oriented languages Every variable is an l-value (denotes location) most functional languages (Lisp, Scheme, ML) Clu, Smalltalk –Java deliberately in-between built-in types are values user-defined types are objects - references

7 Adapted from Scott, 20067 Expression Evaluation Expression-oriented vs. statement-oriented languages –expression-oriented: functional languages (Lisp, Scheme, ML) Algol-68 –statement-oriented: most imperative languages –C kinda halfway in-between (distinguishes) allows expression to appear instead of statement

8 Adapted from Scott, 20068 Expression Evaluation Orthogonality –Features that can be used in any combination –All combinations of features make sense –Meaning of a feature is consistent, regardless of the context in which it is used How about this? if (if b != 0 then a/b == c else false) then... if (if f then true else messy()) then...

9 Adapted from Scott, 20069 Expression Evaluation Assignment –statement (or expression) executed for its side effect –assignment operators (+=, -=, etc) handy avoid redundant work (or need for optimization) perform side effects exactly once A[index(f-4*j)] = A[index(f-4*j)] + 1  A[index(f-4*j)] += 1 –C --, ++ Prefix vs. postfix form Array[k++] vs. Array[++k]

10 Adapted from Scott, 200610 Expression Evaluation Assignment (cont.) –Multiway assignment (e.g., Perl, Python, Ruby) a, b = c, d tuple = tuple –Multiple function return values (e.g., Matlab, G2) a, b, c = funct( x, y );

11 Adapted from Scott, 200611 Expression Evaluation Side Effects –often discussed in the context of functions –a side effect is some permanent state change caused by execution of function some noticeable 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

12 Adapted from Scott, 200612 Expression Evaluation Side effects are fundamental to the whole VonNeuman computing approach In (pure) functional and logic languages, there are no such changes –These languages are called SINGLE- ASSIGNMENT languages

13 Adapted from Scott, 200613 Sequencing –specifies a linear ordering on statements one statement follows another –very imperative, Von-Neuman Sequencing

14 Adapted from Scott, 200614 Selection –sequential if statements if... then... else if... then... elsif... else (cond (C1) (E1) (C2) (E2)... (Cn) (En) (T) (Et) ) Selection

15 Adapted from Scott, 200615 –Fortran computed gotos goto( 15, 20, 30, 100 ) n –Case/switch statements Switch( expression ) { Case 1: 1 st arm of code Case 2: 2 nd arm of code Default: default arm of code } Default clause? List or range of values for each case? Integral values or general values for expression? "Fall through" semantics or not? What if expression evaluates to missing value? Selection

16 Adapted from Scott, 200616 jump code generation 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 –pass inherited attributes INTO expression indicating where to jump to if true, and where to jump to if false Example (follows): if ((A > B) and (C > D)) or (E <> F)) then then_clause else else_clause Selection

17 17 Code generated w/o short-circuiting (Pascal) if ((A > B) and (C > D)) or (E <> F)) r1 := A-- load r2 := B r1 := r1 > r2 r2 := C r3 := D r2 := r2 > r3 r1 := r1 & r2 r2 := E r3 := F r2 := r2 $<>$ r3 r1 := r1 $|$ r2 if r1 = 0 goto L2 L1: then_clause-- label not actually used goto L3 L2: else_clause L3: Selection

18 Adapted from Scott, 200618 Code generated w/ short-circuiting (C) if ((A > B) and (C > D)) or (E <> F)) r1 := A r2 := B if r1 r2 goto L1 L4: r1 := E r2 := F if r1 = r2 goto L2 L1: then_clause goto L3 L2: else_clause L3: Selection

19 Adapted from Scott, 200619 Enumeration-controlled do/for loops –Pascal or Fortran-style do/for loops do 20 i = 1 10... 20 continue scope of control variable? –Local to for loop in C, C++, Java; global in Fortran changes to loop variable or bounds within loop? –Prohibited by Pascal, Fortran 77; permitted by Fortran IV value of i after the loop? –Undefined by Pascal, Fortran IV; most recent for Fortran 77 always executes at least once? –Yes in Fortran, Pascal; no in C, C++, Java Iteration

20 Adapted from Scott, 200620 Iteration The goto controversy –assertion: gotos are needed almost exclusively to cope with 1.lack of one-and-a-half loops –mid-loop exit –multi-level return from loop 2.early return from procedure 3.error returns –Scott: "In many years of programming, I can't remember using one for any other purpose." Usually there is a structured alternative in modern languages 1.break/continue 2.multiple returns 3.exception handling mechanisms

21 Adapted from Scott, 200621 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

22 Adapted from Scott, 200622 Recursion Tail recursion –No computation follows recursive call /* assume a, b > 0 */ int gcd (int a, int b) { if (a == b) return a; else if (a > b) return gcd (a - b, b); else return gcd (a, b – a); }

Download ppt "Adapted from Scott, 20061 Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott."

Similar presentations

Ads by Google