Chapter 6:: Control Flow

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

description

Chapter 6:: Control Flow. Programming Language Pragmatics. Michael L. Scott. Control Flow. Basic paradigms for control flow: Sequencing Selection Iteration Subroutines, recursion (and related control abstractions, e.g. iterators) Nondeterminacy – ordering unspecified - PowerPoint PPT Presentation

Transcript of Chapter 6:: Control Flow

Page 1: Chapter  6:: Control Flow

Adapted from Scott, 2006 1

Chapter 6:: Control Flow

Programming Language PragmaticsMichael L. Scott

Page 2: Chapter  6:: Control Flow

Adapted from Scott, 2006 2

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

Page 3: Chapter  6:: Control Flow

Adapted from Scott, 2006 3

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

Page 4: Chapter  6:: Control Flow

Adapted from Scott, 2006 4

Expression Evaluation

Page 5: Chapter  6:: Control Flow

Adapted from Scott, 2006 5

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 situationsif( b != 0 && a/b == c ) ...if( *p && p->foo ) ...if( f || messy() ) ...

Page 6: Chapter  6:: Control Flow

Adapted from Scott, 2006 6

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

Page 7: Chapter  6:: Control Flow

Adapted from Scott, 2006 7

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

Page 8: Chapter  6:: Control Flow

Adapted from Scott, 2006 8

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 ...

Page 9: Chapter  6:: Control Flow

Adapted from Scott, 2006 9

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 onceA[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]

Page 10: Chapter  6:: Control Flow

Adapted from Scott, 2006 10

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

Page 11: Chapter  6:: Control Flow

Adapted from Scott, 2006 11

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

Page 12: Chapter  6:: Control Flow

Adapted from Scott, 2006 12

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

Page 13: Chapter  6:: Control Flow

Adapted from Scott, 2006 13

• Sequencing– specifies a linear ordering on statements

• one statement follows another

– very imperative, Von-Neuman

Sequencing

Page 14: Chapter  6:: Control Flow

Adapted from Scott, 2006 14

• Selection– sequential if statementsif ... then ... elseif ... then ... elsif ... else(cond(C1) (E1)(C2) (E2)...(Cn) (En) (T) (Et))

Selection

Page 15: Chapter  6:: Control Flow

Adapted from Scott, 2006 15

– Fortran computed gotos• goto( 15, 20, 30, 100 ) n

– Case/switch statementsSwitch( expression ) {

Case 1: 1st arm of codeCase 2: 2nd arm of codeDefault: 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

Page 16: Chapter  6:: Control Flow

Adapted from Scott, 2006 16

• 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)) thenthen_clauseelseelse_clause

Selection

Page 17: Chapter  6:: Control Flow

17

• Code generated w/o short-circuiting (Pascal)if ((A > B) and (C > D)) or (E <> F))

r1 := A -- loadr2 := B

r1 := r1 > r2 r2 := C

r3 := Dr2 := 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 usedgoto L3

L2: else_clause L3:

Selection

Page 18: Chapter  6:: Control Flow

Adapted from Scott, 2006 18

• Code generated w/ short-circuiting (C)if ((A > B) and (C > D)) or (E <> F))

r1 := A r2 := B if r1 <= r2 goto L4 r1 := Cr2 := Dif r1 > r2 goto L1L4: r1 := Er2 := F if r1 = r2 goto L2L1: then_clause goto L3 L2: else_clauseL3:

Selection

Page 19: Chapter  6:: Control Flow

Adapted from Scott, 2006 19

• 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

Page 20: Chapter  6:: Control Flow

Adapted from Scott, 2006 20

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 procedure3. 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/continue2. multiple returns3. exception handling mechanisms

Page 21: Chapter  6:: Control Flow

Adapted from Scott, 2006 21

Recursion

• 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

Page 22: Chapter  6:: Control Flow

Adapted from Scott, 2006 22

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

}