# Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.

## Presentation on theme: "Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements."— Presentation transcript:

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 expressions true || x x || true false && x x && false if (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 e 1 ? e 2 : e 3 ; ML has case-expression: case e 1 of a => e 2 | b => e 3 | c => e 4 ;

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 syntax int if_exp(int x, int y, int z) { if (x) return y; else return z; }

14 Normal Order Evaluation The presence of side effects can substantially change the semantics of an expression int 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 s1 elsif e2 then s2 elsif e3 then s3 end 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) S do 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; 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; … } }

Download ppt "Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements."

Similar presentations