1 / 81

Expressions and Statements

Expressions and Statements. Programming Language Concepts Lecture 16. Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida. 7 Categories of Control Constructs. Sequencing: After A, execute B. A block is a group of sequenced statements. Selection:

Download Presentation

Expressions and Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Expressions and Statements Programming Language Concepts Lecture 16 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

  2. 7 Categories of Control Constructs • Sequencing: • After A, execute B. • A block is a group of sequenced statements. • Selection: • Choice between two (or more) statements. • Iteration: • A fragment is executed repeatedly.

  3. 7 Categories of Control Constructs (cont’d) • Procedural abstraction: • Encapsulate a collection of control constructs in a single unit. • Recursion: • An expression defined in terms of a simpler version of itself. • Concurrency: • Two or more fragments executed at same time.

  4. 7 Categories of Control Constructs (cont’d) • Non-determinacy: • Order is deliberately left unspecified, implying any alternative will work.

  5. Expression Evaluation • Expressions consist of: • Simple object, or • Operator function applied to a collection of expressions. • Structure of expressions: • Prefix (Lisp), • Infix (most languages), • postfix (Postscript, Forth, some calculators)

  6. Expression Evaluation (cont’d) • By far the most popular notation is infix. • Raises some issues. • Precedence: • Specify that some operators, in absence of parentheses, group more tightly than other operators.

  7. Expression Evaluation (cont’d) • Associativity: tie-breaker for operators on the same level of precedence. • Left Associativity: a+b+c evaluated as (a+b)+c • Right Associativity: a+b+c evaluated as a+(b+c) • Different results may or may not accrue: • Generally: (a+b)+c = a+(b+c),but (a-b)-c <> a-(b-c)

  8. Expression Evaluation (cont’d) • Specify evaluation order of operators. • Generally, left-to-right (Java), but in some languages, the order is implementation-defined (C).

  9. Operators and Precedence in Various Languages • C is operator-richer than most languages • 17 levels of precedence. • some not shown in figure: • type casts, • array subscripts, • field selection (.) • dereference and field selection • a->b, equivalent to (*a).b • Pascal:<, <=, …, in (row 6)

  10. Pitfalls in Pascal if a < b and c < d then ... is parsed as if a < (b and c) < d. Will only work if a,b,c are booleans.

  11. Pitfalls in C a < b < c parsed as (a < b) < c, yielding a comparison between (a < b)(0 or 1) and c.

  12. Assignments • Functional programming: • We return a value for surrounding context. • Value of expression depends solely on referencing environment, not on the time in which the evaluation occurs. • Expressions are "referentially transparent."

  13. Assignments (cont’d) • Imperative: • Based on side-effects. • Influence subsequent computation. • Distinction between • Expressions (return a value) • Statements (no value returned, done solely for the side-effects).

  14. Variables • Can denote a location in memory (l-value) • Can denote a value (r-value) • Typically, 2+3 := c; is illegal, as well as c := 2+3; if c is a declared constant.

  15. Variables (cont’d) • Expression on left-hand-side of assignment can be complex, as long as it has an l-value: (f(a)+3)->b[c] = 2; in C. • Here we assume f returns a pointer to an array of elements, each of which is a structure containing a field b, an array. Entry c of b has an l-value.

  16. Referencing/Dereferencing • Consider b := 2; c := b; a := b + c; • Value Model Reference Model

  17. Referencing/Dereferencing (cont’d) • Pascal, C, C++ use the "value model": • Store 2 in b • Copy the 2 into c • Access b,c, add them, store in a. • Clu uses the "reference" model: • Let b refer to 2. • Let c also refer to 2. • Pass references a,b to "+", let a refer to result.

  18. Referencing/Dereferencing (cont’d) • Java uses value model for intrinsic (int, float, etc.) (could change soon !), and reference model for user-defined types (classes)

  19. Orthogonality • Features can be used in any combination • Every combination is consistent. • Algol was first language to make orthogonality a major design goal.

  20. Orthogonality In Algol 68 • Expression oriented; no separate notion of statement. begin a := if b < c then d else e; a := begin f(b); g(c) end; g(d); 2+3 end

  21. Orthogonality In Algol 68 (cont’d) • Value of 'if' is either expression (d or e). • Value of 'begin-end' block is value of last expression in it, namely g(c). • Value of g(d) is obtained, and discarded. • Value of entire block is 5.

  22. Orthogonality In Algol 68 (cont’d) • C does this as well: • Value of assignment is value of right-hand-side: c = b= a++;

  23. Pitfall in C if (a=b) { ... } /* assign b to a and proceed */ /* if result is nonzero /* • Some C compilers warn against this. • Different from if (a==b) { ... } • Java has separate boolean type: • prohibits using an int as a boolean.

  24. Initialization • Not always provided (there is assignment) • Useful for 2 reasons: • Static allocation: • compiler can place value directly into memory. • No execution time spent on initialization. • Variable not initialized is common error.

  25. Initialization (cont’d) • Pascal has NO initialization. • Some compilers provide it as an extension. • Not orthogonal, provided only for intrinsics. • C, C++, Ada allow aggregates: • Initialization of a user-defined composite type.

  26. Example: (C) int a[] = {2,3,4,5,6,7} • Rules for mismatches between declaration and initialization: int a[4] = {1,2,3}; /* rest filled with zeroes */ int a[4] = {0}; /* filled with all zeroes */ int a[4] = {1,2,3,4,5,6,7} /* oops! */ • Additional rules apply for multi-dimensional arrays and structs in C.

  27. Uninitialized Variables • Pascal guarantees default values (e.g. zero for integers) • C guarantees zero values only for static variables, *garbage* for everyone else !

  28. Uninitialized Variables (cont’d) • C++ distinguishes between: • initialization (invocation of a constructor, no initial value is required) • Crucial for user-defined ADT's to manage their own storage, along with destructors. • assignment (explicit)

  29. Uninitialized Variables (cont’d) • Difference between initialization and assignment: variable length string: • Initialization: allocate memory. • Assignment: deallocate old memory AND allocate new.

  30. Uninitialized Variables (cont’d) • Java uses reference model, no need for distinction between initialization and assignment. • Java requires every variable to be "definitely assigned", before using it in an expression. • Definitely assigned: every execution path assigns a value to the variable.

  31. Uninitialized Variables (cont’d) • Catching uninitialized variables at run-time is expensive. • harware can help, detecting special values, e.g. "NaN" IEEE floating-point standard. • may need extra storage, if all possible bit patterns represent legitimate values.

  32. Combination Assignment Operators • Useful in imperative languages, to avoid repetition in frequent updates: a = a + 1; b.c[3].d = b.c[3].d * 2; /* ack ! */ • Can simplify: ++a; b.c[3].d *= 2;

  33. Combination Assignment Operators (cont’d) • Syntactic sugar for often used combinations. • Useful in combination with autoincrement operators: A[--i]=b; equivalent to A[i -= 1] = b;

  34. Combination Assignment Operators (cont’d) *p++ = *q++; /* ++ has higher precedence than * */ equivalent to *(t=p, p += 1, t) = *(t=q, q += 1, t); • Advantage of autoincrement operators: • Increment is done in units of the (user-defined) type.

  35. Comma Operator • In C, merely a sequence: int a=2, b=3; a,b = 6; /* now a=2 and b=6 */ int a=2, b=3; a,b = 7,6; /* now a=2 and b=7 */ /* = has higher precedence than , */

  36. Comma Operator • In Clu, "comma" creates a tuple: a,b := 3,4 assigns 3 to a, 4 to b a,b := b,a swaps them ! • We already had that in RPAL: let t=(1,2) in (t 2, t 1)

  37. Ordering within Expressions • Important for two reasons: • Side effect: • One sub expression can have a side effect upon another subexpression: (b = ++a + a--) • Code improvement: • Order evaluation has effect on register/instruction scheduling.

  38. Ordering within Expressions (cont’d) • Example: a * b + f(c) • Want to call f first, avoid storing (using up a register) for a*b during call to f.

  39. Ordering within Expressions (cont’d) • Example: a := B[i]; c := a * 2 + d * 3; • Want to calculate d * 3 before a * 2: Getting a requires going to memory (slow); calculating d * 3 can proceed in parallel.

  40. Ordering within Expressions (cont’d) • Most languages leave subexpression order unspecified (Java is a notable exception, uses left-to-right) • Some will actually rearrange subexpressions.

  41. Example (Fortran) a = b + c c = c + e + b rearranged as a = b + c c = b + c + e and then as a = b + c c = a + e

  42. Rearranging Can Be Dangerous • If a,b,c are close to the precision limit (say, about ¾ of largest possible value), then a + b - c will overflow, whereas a - c + b will not. • Safety net: most compilers guarantee to follow ordering imposed by parentheses.

  43. Short Circuit Evaluation • As soon as we can conclude outcome of the evaluation, skip the remainder of it. • Example (in Java): if ( list != null && list.size() != 0)) System.out.println(list.size()); • Will never throw null pointer exception

  44. Short Circuit Evaluation (cont’d) • Can't do this in Pascal: if (list <> nil) and (list^.size <> 0) • will evaluate list^.size even when list is nil. • Cumbersome to do it in Pascal: if list <> = nil then if list^.size <> 0 then System.out.println(list.size());

  45. Short Circuit Evaluation (cont’d) • So, is short-circuit evaluation always good? • Not necessarily.

  46. Short Circuit Evaluation (cont’d)

  47. Short Circuit Evaluation (cont’d) • Here, the idea is to tally AND to spell-check every word, and print the word if it's misspelled and has appeared for the 10th time. • If the 'and' is short-circuit, the program breaks. • Some languages (Clu, Ada, C) provide BOTH short-circuit and non short-circuit Boolean operators.

  48. Structured Programming • Federal Law: Abandon Goto's ! • Originally, Fortran had goto's: if a .lt. b goto 10 ... 10

  49. Structured Programming (cont’d) • Controversy surrounding Goto's: • Paper (letter to editor ACM Comm.) in 1968 by E. Dykstra: • "Goto statement Considered Harmful" • argument: Goto's create "spaguetti code".

More Related