1 / 26

Expressions and Statements

This article explores the concepts of expressions and statements, including side effects, expression notations, evaluation orders, conditional statements, and loops. It also discusses the impact of side effects on expression evaluation and the use of operators with side effects. Additionally, it covers delayed evaluation, if and case expressions, referential transparency, and normal order evaluation. The article concludes with a discussion on if and case statements and loop statements.

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

  2. Contents • Side effects: expressions and statements • Expression notations • Expression evaluation orders • Conditional statements • Loops • Unstructured statements

  3. Side Effects • Expressions and statements represent the most fundamental computation mechanisms • An expression, in its pure form, returns a value and produces no side effect • A statement is mainly executed for its side effect and returns no value change variable values, do input or output

  4. Expression Notations • Operators can be written in infix, postfix, or prefix notation 3 + 4 * 5 3 4 5 * + + 3 * 4 5 • No operator precedence and associativity are required for postfix and prefix notations • Lisp uses prefix notation • Postscript and FORTH use postfix notation

  5. Applicative Order Evaluation • Applicative order (or strict) evaluation evaluates operands before operators (3 + 4) * (5 – 6) • The evaluation order for operands is usually not specified (3 + 4) * (5 – 6)

  6. An Example int x = 1; int f(void) { x += 1; return x; } int p( int a, int b) { return a + b; } main() { printf("%d\n",p( x, f() )); return 0; } f() produces side effect order of evaluation matters

  7. Operators with side effects • Sometimes expressions are explicitly constructed with side effects in mind • C is an expression-oriented language. In C, assignment is an expression, not a statement x = y = z; • In C, sequence operator is used to combine several expressions (with side effects) into a single expression x = (y += 1, x += y, x + 1);

  8. Delayed Evaluation • The evaluation of an expression can sometimes be performed without the evaluation of all its subexpressions. This is called delayed (or non-strict) evaluation • Short-circuit evaluation of Boolean expressionstrue || x x || truefalse && x x && falseif (i <= lastindex && a[i] >= x) …if (p != NULL && p->data == x) …if (x != 0 and y % x == 0) …

  9. if and case Expressions • An if-expression always evaluates the test-expression, but based on that value, only one of the then-expression or else-expression is ever evaluated e1? e2: e3; • ML has case-expression:case e1of a => e2 | b => e3 | c => e4 ;

  10. Referential Transparency • In the absence of side effects, the order of evaluation of subexpressions is immaterial to the final value of the expression • Such expressions share an important property with mathematical expressions, called referential transparency • Any two expressions in a program that have the same value may be substituted for each other anywhere in the program • Variables would be unknown constants

  11. Normal Order Evaluation • Referential transparency allows for a very strong form of delayed evaluation to be used • Normal order evaluation evaluates its operator before its operands and each operand is evaluated only it is needed for the calculation of the value of the operation

  12. An Example int double(int x) { return x + x; } int square(int x) { return x * x; } • square(double(2)) • double(2) * double(2) • (2 + 2) * (2 + 2) • 4 * (2 + 2) • 4 * 4 • 16

  13. Normal Order Evaluation • Normal order evaluation might seem inefficient. It can be made efficient (2 + 2) * (2 + 2) • Delayed evaluation does not need special syntaxint if_exp(int x, int y, int z) {if (x) return y;elsereturn z; }

  14. Normal Order Evaluation • The presence of side effects can substantially change the semantics of an expressionint get_int(void) { int x; scanf(“%d”, &x); return x; } square(get_int()); get_int() * get_int();

  15. If statements if-statement  if ( expression ) statement [ else statement ] if-statement  if expression then statement [ else statement ] end if ; if e1 then s1elsif e2 then s2elsif e3 then s3end if; C Ada

  16. Case-Statements switch (x - 1) { case 0: y = 0; z = 2; break; case 2: case 3: case 4: case 5: y = 3; z = 1; break; case 7: case 9: z = 10; break; default: break; }

  17. Case-Statements case x - 1 is when 0 => y := 0; z := 2; when 2 .. 5 => y := 3; z := 1; when 7 | 9 => z := 10; when others => null; end case;

  18. Guarded If Statements • Guarded if statement: if B1 -> S1 | B2 -> S2 … | Bn -> Sn fi • If one of the Bi’s evaluates to true, then Si is executed. If more than one of the Bi’s is true, then one and only one Si is selected for execution. If none of the Bi is true, then an error occurs Nondeterminism

  19. Loop-Statements while (e) Sdo S while (e);for (e1; e2; e3) S;

  20. Restrictions on Index Variable of For Loop • The value of i cannot be changed within the body of the loop • The value of i is undefined after the loop terminates • i must be of restricted type and may not be declared in certain way

  21. Questions about For Loop • Are the bounds evaluated only once • If the lower bound is greater than the upper bound, is the loop executed at all • Is the value of the index variable undefined even if an exit or break statement is used to leave the loop before termination • What translator checks are performed on loop structure

  22. Guarded Loops • Guarded do statement: do B1 -> S1 | B2 -> S2 … | Bn -> Sn od • This statement is repeated until all the Bi’s are false. At each step, one of the true Bi’s is selected nondeterministically, and Si is executed

  23. CLU Iterators numcount = proc (s: string) returns (int); count: int = 0; for c: char in stringchars(s) do if numeric (c) then count := count + 1; end; end; return (count); end numcount;

  24. CLU Iterators stringchars = iter (s: string) yields (char); index: int := 1; limit: int := string$size(s); while index <= limit do yield (string$fetch(s, index)) index := index + 1; end; end stringchars;

  25. The goto Controversy • Bohm and Jacopini (1966) demonstrated that every possible control structure could be expressed using structured control constructs, i.e., gotos are unnecessary • Dijkstra (1968) argued that the use of gotos was damaging in many ways • Rubin (1987) argued that the use of gotos was justified in certain cases

  26. Labeled Break in Java if (ok) { while (more) { … while (!found) { … if (disaster) goto 99; … } } } 99: ok_break: if (ok) { while (more) { … while (!found) { … if (disaster) break ok_break; … } } }

More Related