Expressions and Statements

26
Expressions and Statements

description

Expressions and Statements. Contents. Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements Loops Unstructured statements. Side Effects. Expressions and statements represent the most fundamental computation mechanisms - PowerPoint PPT Presentation

Transcript of Expressions and Statements

Page 1: Expressions and Statements

Expressions and Statements

Page 2: Expressions and Statements

2

Contents

• Side effects: expressions and statements

• Expression notations

• Expression evaluation orders

• Conditional statements

• Loops

• Unstructured statements

Page 3: Expressions and 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

Page 4: Expressions and Statements

4

Expression Notations

• Operators can be written in infix, postfix, or prefix notation

3 + 4 * 53 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

Page 5: Expressions and Statements

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)

Page 6: Expressions and Statements

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

Page 7: Expressions and Statements

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

Page 8: Expressions and Statements

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 || xx || truefalse && xx && falseif (i <= lastindex && a[i] >= x) …if (p != NULL && p->data == x) …if (x != 0 and y % x == 0) …

Page 9: Expressions and Statements

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 e1 of a => e2 | b => e3 | c => e4 ;

Page 10: Expressions and Statements

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

Page 11: Expressions and Statements

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

Page 12: Expressions and Statements

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

Page 13: Expressions and Statements

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; else return z; }

Page 14: Expressions and Statements

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

Page 15: Expressions and Statements

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

Page 16: Expressions and Statements

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

Page 17: Expressions and Statements

17

Case-Statementscase 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;

Page 18: Expressions and Statements

18

Guarded If Statements• Guarded if statement:

if B1 -> S1| B2 -> S2 …| Bn -> Snfi

• 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

Page 19: Expressions and Statements

19

Loop-Statements

while (e) S

do S while (e);

for (e1; e2; e3) S;

Page 20: Expressions and Statements

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

Page 21: Expressions and Statements

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

Page 22: Expressions and Statements

22

Guarded Loops

• Guarded do statement:do B1 -> S1| B2 -> S2 …| Bn -> Snod

• 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

Page 23: Expressions and Statements

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;

Page 24: Expressions and Statements

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;

Page 25: Expressions and Statements

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

Page 26: Expressions and Statements

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; … } }}