Control FlowControl Flow --- ParadigmsOperatorsPrefix, Infix, PostfixSmalltalk --- Binary MessagesSmalltalk --- Keyword MessagesOperator PrecedenceOperator AssociativityCase Study --- CCase Study --- Cldots VariablesValue vs. Reference ModelValue vs. Reference Modelldots ExpressionsOrder of EvaluationOrder of Evaluationldots Case Study --- PascalControl-Flow StatementsStatement vs. Expression OrientationUnstructured Control-FlowCase Study --- Pascal: gotoStatements --- SelectionCase Study --- Pascal: ifCase Study --- Modula-2: ifCase Study --- Pascal: caseCase Study --- C: caseCase Study --- FORTRAN: gotoStatements --- IterationCase Study --- Pascal: forCase Study --- Modula-2: FORCase Study --- Modula-3: FORCase Study --- Modula-3: FORCase Study --- Modula-3: FORldots Case Study --- Pascal: loopsCase Study --- Modula-2: loopsCase Study --- Algol 60Case Study --- Algol 60ldots RecursionTail RecursionTail Recursionldots Tail Recursionldots Tail Recursionldots Readings and References520 —Spring 2008 — 26CSc 520Principles of ProgrammingLanguages26 : Control Structures — IntroductionChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2008 Christian Collberg[1]520 —Spring 2008 — 26Control FlowWe need some way of ordering computations:sequencingselectioniterationprocedural abstraction — being able to treat acollection of other control constructs as a single unit, asubroutine.recursionconcurrencynondeterminacy — being able to explcitly state that theordering between two statements is uspecified, and,possibly should be selected randomly/fairly.[2]520 —Spring 2008 — 26Control Flow — ParadigmsFunctional languages — recursion and selection areimportant, iteration and sequencing not.Procedural languages — iteration, sequencing,selection are important, recursion not.Logic languages — the programmer gives rules thatrestrict control flow, the interpreter deduces anexecution ordering that satisifes these rules.[3]520 —Spring 2008 — 26Operators[4]520 —Spring 2008 — 26Prefix, Infix, PostfixLanguages use prefix, infix, or postfix notation foroperators in expressions.This means that the operator comes before, among, orafter its operands.Lisp/Scheme uses Cambridge Polish notation (avariant of prefix):(*(+ 5 6) 7)Postscript and Forth use postfix notation.Smalltalk uses infix notation.[5]520 —Spring 2008 — 26Smalltalk — Binary MessagesA binary message M to receiver R with argument A hasthe syntaxR M AFor example:8 + 9This sends the message + to the object 8 with theargument 9.[6]520 —Spring 2008 — 26Smalltalk — Keyword MessagesA keyword message M to receiver R with argumentsA1, A2, A3, . . . has the syntaxR M1: A1M2: A2M3: A3...For example:DeannaTroi kiss: cheek how: tenderlyThis sends the message kiss:how: to the objectDeannaTroi with the arguments cheek andtenderly. In Java we would have written:DeannaTroi.kisshow(cheek,tenderly)[7]520 —Spring 2008 — 26Operator PrecedenceThe precedence of an operator is a measure of itsbinding power, i.e. how strongly it attracts its operands.Usually ∗ has higher precedence than +:4 + 5 ∗ 3means4 + (5 ∗ 3),not(4 + 5) ∗ 3.We say that ∗ binds harder than +.[8]520 —Spring 2008 — 26Operator AssociativityThe associativity of an operator describes howoperators of equal precedence are grouped.+ and − are usually left associative:4 − 2 + 3means(4 − 2) + 3 = 5,not4 − (2 + 3) = −1.We say that +associates to the left.ˆ associates to the right:2ˆ3ˆ4 = 2ˆ(3ˆ4).[9]520 —Spring 2008 — 26Case Study — CC has so many rules for precedence and associativitythat most programmers don’t know them all.See the table on the next slide.[10]520 —Spring 2008 — 26Case Study — C...OPERATOR KIND PREC ASSOCa[k] Primary 16f(· · · ) Primary 16. Primary 16-> Primary 16a++, a-- Postfix 15++a, --a Unary 14˜ Unary 14! Unary 14- Unary 14& Unary 14*Unary 14OPERATOR KIND PREC ASSOC*, /, %Binary 13 Left+, - Binary 12 Left<<, >> Binary 11 Left<, >, <=, >= Binary 10 Left== != Binary 9 Left& Binary 8 Leftˆ Binary 7 Left| Binary 6 Left&& Binary 5 Left|| Binary 4 Left? : Ternary 3 Right=, +=, -=,*=,/=, %=, <<=,>>=, &=, ˆ=, |=Binary 2 Right,Binary 1 Left[11]520 —Spring 2008 — 26Variables[12]520 —Spring 2008 — 26Value vs. Reference Modell-value — an expression that denotes a location, suchas the left-hand side in x:=..., x[i]:=...,x.a[i]->v:=....r-value — an expression that denotes a value, such asthe right-hand side in ...:=x, ...:=x[i],...:=x.a[i]->v, ...:=3+x.Pascal, C, Ada use a value model of variables. In...:=x, x refers to the value stored in x.Clu (and other languages) use a reference model forvariables. In ...:=x, x is a reference to the valuestored in x.[13]520 —Spring 2008 — 26Value vs. Reference Model...In Pascal, after the statementsb := 2;c := b;both b and c would hold the value 2. In Clu, b and cwould both point to the same object, which contains thevalue 2.Java uses a value model for int, float, etc, but areference model for String. Henceint i,j;String s,t;if (i==j) ...if (s==t) ...can be confusing for novel programmers.[14]520 —Spring 2008 — 26Expressions[15]520 —Spring 2008 — 26Order of EvaluationMany languages allow the compiler to reorderoperations in an expression, for efficiency.Java requires strict left-to-right evaluation. Why?If the expression (b,c,d are 32-bit ints)b-c+dis reordered asb+d-cthen an overflow can occur if b+d doesn’t fit in an int.[16]520 —Spring 2008 — 26Order of Evaluation...Let a,b,c be 32-bit floats, where a is small, b,c arelarge, and b=-c.Then the expression(a+b)+cmight evaluate to 0 (due to a loss of information), whilea+(b+c)would evaluate to a.[17]520 —Spring 2008 — 26Case Study — PascalPascal does not use short-circuit evaluation. Hence,this makes for problems:if (x<>0) and (y/x > 5) thenPascal has non-intuitive precedence:4 > 8 or 11 < 3is parsed as4 > (8 or 11) < 3Hence, it becomes necessary to insert parenthesis.[18]520 —Spring 2008 — 26Control-Flow Statements[19]520 —Spring 2008 — 26Statement vs. Expression OrientationIn Pascal, Ada, Modula-2, if, while, etc. arestatements. This means that they are executed for theirside-effects only, and return no value.In Algol68 if, while, etc. are expressions, they canhave both side-effects and return values:beginx := if b<c then d else e;y := begin f(b); g(c) end;z := while b<c do g(c) end;2+3endThis compound block returns
View Full Document